This is my interface
interface X {
key: string
value: number | undefined
default?: number
}
But I want the non-optional keys only, aka. "key" | "value"
, or just "key"
(both will do fine for me)
type KeyOfX = keyof X
gives me "key" | "value" | "default"
.
type NonOptionalX = {
[P in keyof X]-?: X[P]
}
type NonOptionalKeyOfX = keyof NonOptionalX
gives "key" | "value" | "default"
as -?
only removes the optional modifier and make all of them non-optional.
ps. I use Typescript 2.9.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…