#Conditional typing

1 messages · Page 1 of 1 (latest)

burnt flare
#

Just curious if its possible
So for example i have a type

type Props = {
  zoom?:boolean,
  options?:Record<string,any>
}

Is it possible if value of zoom is true then options is present in type and if zoom is false then options would be never?

simple berry
#

use a discriminated union

#

what if zoom is missing though?

#

(sidenote; never is different to not being present)

rose flint
#

!hb isc

ripe gardenBOT
rose flint
#

bruh

#

!hb disc

ripe gardenBOT
burnt flare
# simple berry what if `zoom` is missing though?

Im just rendering different templates
If zoom is true then renders a wrapper which has options props and if not then im having different thing
The idea was if i pass zoom prop then i get type suggestion for options and if not i would not get them, its totally not necessary but i was just wondering if its possible to make

simple berry
#

lmao

rose flint
simple berry
burnt flare
#

I just not sure what way to go

rose flint
#

i mean, thats fine too

burnt flare
#

either have the type all the time

#

or not

simple berry
#

would be ts type Props = { zoom: true; options: object; } | { zoom?: false; }then

rose flint
#

yeah

burnt flare
#

oh dang

rose flint
#

i actually use this pattern for multiple?: false in some react components

burnt flare
#

never thought about this that way