#How can I know if an optional property exists in the type declaration?

3 messages · Page 1 of 1 (latest)

austere vaporBOT
#
settingdust#0

Preview:```ts
interface A {
test?: string
}

type DefaultTest = "test"

function test<T extends A>(a: T) {
type ActualTest = T["test"] extends undefined
? DefaultTest
: T["test"]
return "" as ActualTest
}

// Should be "test". But it's string | undefined.
type ResultA = ReturnType<typeof test>
...```

prime pier
#

You got the condition backwards, it should be undefined extends T['test'].