#Type inference with switch/case

21 messages · Page 1 of 1 (latest)

harsh hornet
#

I want the component properties change based on it's type. How to work around this?

tranquil frost
#

make it a discriminated union instead of that

harsh hornet
#

Could you elaborate what you mean by that, I'm very basic at TS

tranquil frost
#

!hb discriminated union

radiant fieldBOT
tranquil frost
#

sorry i forgot that lol ^

void stirrup
#

@harsh hornet i'm pretty sure the function doesn't know about the type annotation that's all the way on the other side of the =

#

you probably want to make the actual function generic

#

if you want the type ContentFc you'd still be able to do that by doing type ContentFc = typeof Content;

harsh hornet
#

I think i did the right think @tranquil frost

void stirrup
#

oh right

#

i forgot that discriminated unions don't need generics

#

yeah if there are no errors it should be working

harsh hornet
#

I don't know what I've done but it's not throwing error

#

😄

void stirrup
#

yeah typescript automatically narrows discriminated unions (unions with a property that has a different value in different union members)

harsh hornet
#

okey

void stirrup
#

in your case it's .type

harsh hornet
#

ty you guys for your precious time