#Using type from optional object value

10 messages · Page 1 of 1 (latest)

main vesselBOT
#
drwarpman#0

Preview:```ts
type Foobar = {
foo?: "bar"
}

const test = "whatever" as unknown as Foobar["foo"]
//^?```

torpid mirage
#

test shows it can be "bar" | undefined how can I make it be just "bar" ?

#

my only solution i could find is to put Foobar into Required, like this:

const test = "whatever" as unknown as Required<Foobar>["foo"];
#

is this the right way?

fiery granite
torpid mirage
#

Hm, it actually doesnt?

supple oracle
torpid mirage
#

ohi m stupid

#

sorry