Skip to content

Commit

Permalink
tests: add unit tests for isMatching's inference
Browse files Browse the repository at this point in the history
  • Loading branch information
gvergnaud committed Feb 2, 2024
1 parent e707bf7 commit 4bd508c
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions tests/is-matching.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,26 @@ describe('isMatching', () => {
);
}
});

it('type inference should be precise without `as const`', () => {
type Pizza = { type: 'pizza'; topping: string };
type Sandwich = { type: 'sandwich'; condiments: string[] };
type Food = Pizza | Sandwich;

const food = { type: 'pizza', topping: 'cheese' } as Food;

const isPizza = isMatching({ type: 'pizza' });

if (isPizza(food)) {
type t = Expect<Equal<typeof food, Pizza>>;
} else {
throw new Error('Expected food to match the pizza pattern!');
}

if (isMatching({ type: 'pizza' }, food)) {
type t = Expect<Equal<typeof food, Pizza>>;
} else {
throw new Error('Expected food to match the pizza pattern!');
}
});
});

0 comments on commit 4bd508c

Please sign in to comment.