#How to check if union type can be undefined or not using extends?

7 messages · Page 1 of 1 (latest)

undone apex
#

Given a type type MyType = string | undefined how do I type check if the type can be undefined? I tried the following but it doesn't work. Any idea whats the correct way to do this?

type IsUndefined = MyType extends undefined ? 'yes' : 'no'
// no
humble violetBOT
#
krimson#7581

Preview:```ts
type MyType = string | undefined

type IsUndefined = MyType extends undefined
? "yes"
: "no"
// ^?```

fickle sinew
#

@undone apex MyType extends undefined checks if your type is a subtype of undefined - you'd want the reverse: undefined extends MyType ? 'yes' : 'no'

#

This checks if undefined is a subtype of your type.

undone apex
#

oh duh that makes sense! Thank you

#

!solved

fickle sinew
#

!resolved