Typescript Determining type of generic.

Hi, I have the following generic function
const handleContinue = <T extends A | B>(data: T) => {}
const handleContinue = <T extends A | B>(data: T) => {}

How can I check inside this function when data = A, or when data = B?
3 Replies
Xaohs
Xaohs2y ago
I'd like to do something along the lines of
if (data === typeof A) { ..... }
if (data === typeof B) { ..... }
if (data === typeof A) { ..... }
if (data === typeof B) { ..... }
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Xaohs
Xaohs2y ago
Hmm not the cleneast solution but it works I guess. I did it as follows
function isA(data: A | B): data is A{
return (data as A).property !== undefined;
}
function isA(data: A | B): data is A{
return (data as A).property !== undefined;
}
And i just type check it by isA(data)
Want results from more Discord servers?
Add your server