Skip to content

Commit

Permalink
fix(P.nonNullable): add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gvergnaud committed Apr 6, 2024
1 parent e1272af commit 8b928dd
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 35 deletions.
2 changes: 1 addition & 1 deletion src/types/ExtractPreciseValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export type ExtractPreciseValue<a, b> = b extends Override<infer b1>
? never
: // An empty object `{}` in a pattern means
// that this key must be non-nullable.
// If find a key in `b` that doesn't exist in `a`
// If we find a key in `b` that doesn't exist in `a`
// and that contains `{}`, then the pattern does not match.
Contains<Omit<b, keyof a>, {}> extends true
? never
Expand Down
51 changes: 51 additions & 0 deletions tests/extract-precise-value.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { ExtractPreciseValue } from '../src/types/ExtractPreciseValue';
import { InvertPattern } from '../src/types/InvertPattern';
import { NonNullablePattern } from '../src/types/Pattern';
import { Expect, Equal } from '../src/types/helpers';
import { AsyncResult, Event, Option, State } from './types-catalog/utils';

Expand Down Expand Up @@ -292,6 +294,55 @@ describe('ExtractPreciseValue', () => {
});
});

describe('non-nullable patterns', () => {
type nonNullable = InvertPattern<NonNullablePattern, unknown>;

it('should exclude objects if the absent', () => {
type res1 = ExtractPreciseValue<{ a: string }, { b: nonNullable }>;
type test1 = Expect<Equal<res1, never>>;

type res2 = ExtractPreciseValue<
{ a: string } | { b: number },
{ b: nonNullable }
>;
type test2 = Expect<Equal<res2, { b: number }>>;

type res3 = ExtractPreciseValue<
{ a: string } | { b: number } | { b: string; c: boolean },
{ b: nonNullable }
>;
type test3 = Expect<
Equal<res3, { b: number } | { b: string; c: boolean }>
>;
});

it('should keep empty objects if they come from the input type', () => {
type res1 = ExtractPreciseValue<
{ a: string } | { b: {} },
{ b: nonNullable }
>;
type test1 = Expect<Equal<res1, { b: {} }>>;
});

it('should exclude objects even if the non-nullable key is deeply nested', () => {
type res1 = ExtractPreciseValue<{ a: number }, { b: { c: nonNullable } }>;
type test1 = Expect<Equal<res1, never>>;

type res2 = ExtractPreciseValue<
| { nested: { a: string } }
| { nested: { b: number } }
| { nested: { b: string; c: boolean } },
{ nested: { b: nonNullable } }
>;
type test2 = Expect<
Equal<
res2,
{ nested: { b: number } } | { nested: { b: string; c: boolean } }
>
>;
});
});

describe('Branded strings', () => {
it('Type narrowing should correctly work on branded strings', () => {
// Branded strings is a commonly used way of implementing
Expand Down
42 changes: 8 additions & 34 deletions tests/wildcards.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,7 @@ describe('wildcards', () => {
type Input =
| {
__typename: 'ValidationRejection';
fields: {
__typename: 'ValidationFieldError';
path: string[];
message: string;
}[];
fields: string[];
}
| {
__typename: 'ValidationRejection';
Expand All @@ -106,38 +102,16 @@ describe('wildcards', () => {
.with(
{ __typename: 'ValidationRejection', fields: P.nonNullable },
({ fields }) => {
type t = Expect<
Equal<
typeof fields,
{
__typename: 'ValidationFieldError';
path: string[];
message: string;
}[]
>
>;
type t = Expect<Equal<typeof fields, string[]>>;
return 'matched';
}
)
.otherwise(() => {});
.otherwise(() => 'did not match');

const fn2 = (data: Input) =>
match(data)
.with(
{ __typename: 'ValidationRejection', fields: P.not(P.nullish) },
({ fields }) => {
type t = Expect<
Equal<
typeof fields,
{
__typename: 'ValidationFieldError';
path: string[];
message: string;
}[]
>
>;
}
)
.otherwise(() => {});
expect(fn({ __typename: 'ValidationRejection' })).toBe('did not match');
expect(fn({ __typename: 'ValidationRejection', fields: [] })).toBe(
'matched'
);
});
});

Expand Down

0 comments on commit 8b928dd

Please sign in to comment.