How to match a custom type/interface? #115
Unanswered
KlausJokisuo
asked this question in
Q&A
Replies: 1 comment 4 replies
-
Hey! The recommended way to achieve this is to create a user pattern and then infer the type from the pattern: const userPattern = {
age: P.number,
name: P.string,
uid: P.string,
};
type User = P.infer<typeof userPattern>;
type NullOrUser = User | null;
const nullOrUser: NullOrUser = {
age: 10,
name: 'Fuu',
uid: 'Faa',
};
match(nullOrUser)
.with(userPattern, () => console.log('It´s User'))
.with(P.nullish, () => console.log('It´s null'))
.exhaustive(); |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Heya,
I was wondering how I could check if the match is the correct type.
I know that I can check if the match is null, but I would like to check the correct type too.
Thanks 👍
Beta Was this translation helpful? Give feedback.
All reactions