Replaces undefined in Union Types with null
undefined
null
interface FooBar { foo?: string, bar?: number, spam: boolean}function foobar<K extends keyof Foobar>(key: K, obj: FooBar) { return (obj[key] ?? null) as Nullify<FooBar[K]>}const FooBarVar: FooBar = { foo: '420', bar: 69, spam: true}foobar('foo', FooBar) // string | nullfoobar('bar', FooBar) // number | nullfoobar('spam', FooBar) // boolean
Generated using TypeDoc
Replaces
undefined
in Union Types withnull
Example