Skip to content

Commit 4bd508c

Browse files
committed
tests: add unit tests for isMatching's inference
1 parent e707bf7 commit 4bd508c

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

tests/is-matching.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,26 @@ describe('isMatching', () => {
5555
);
5656
}
5757
});
58+
59+
it('type inference should be precise without `as const`', () => {
60+
type Pizza = { type: 'pizza'; topping: string };
61+
type Sandwich = { type: 'sandwich'; condiments: string[] };
62+
type Food = Pizza | Sandwich;
63+
64+
const food = { type: 'pizza', topping: 'cheese' } as Food;
65+
66+
const isPizza = isMatching({ type: 'pizza' });
67+
68+
if (isPizza(food)) {
69+
type t = Expect<Equal<typeof food, Pizza>>;
70+
} else {
71+
throw new Error('Expected food to match the pizza pattern!');
72+
}
73+
74+
if (isMatching({ type: 'pizza' }, food)) {
75+
type t = Expect<Equal<typeof food, Pizza>>;
76+
} else {
77+
throw new Error('Expected food to match the pizza pattern!');
78+
}
79+
});
5880
});

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy