You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
keyof is a type operator. Note, name change from Keysof type operator #10425 to keyof; since the type name represents one value and not the whole value domain. this is inline with the other names of types e.g. we call number and not numbers,
Henceforth, formal names of these type operators:
keyof T => Index Type Query
T[K] => Indexed Access Type
Details
keyof T
known properties and index signatures (number | string for string indexers and number for numeric indexers)
symbols not supported yet. we need to make symbols literal types first.
private and protected
A piece of trivia, accessing private and protected properties are allowed for index access today
classC{privatex;}letc: C;c.x;// not allowedc["x"];// is allowed today
should this apply to the new type operator: ts type T = C["x"]; // OK or not?
for
nameof in C# does not do this either
it exists at run time
against
we should not carry past misdeeds in new features
it exposes implementation details
not valid for declaration emit, since we strip out the type
Decision
keyof T is public-only known properties, private and protected properties are not included
T[K]
only index with a type K parameter iff it is constraint to the keyof T
this grantees that the output is type of a property
can be used with literal types
classThing{name: string;width: number;}typeq1=Thing["name"];// stringtypeq2=Thing["name"|"width"];//string | numbertypeq3=Thing["name"|"foo"];// Error "foo" is not a property on Thing
keyof T
andT[K]
Note about terminology
keyof
is a type operator. Note, name change from Keysof type operator #10425 tokeyof
; since the type name represents one value and not the whole value domain. this is inline with the other names of types e.g. we callnumber
and notnumbers
,keyof T
=> Index Type QueryT[K]
=> Indexed Access TypeDetails
keyof T
known properties and index signatures (
number | string
for string indexers andnumber
for numeric indexers)Examples:
keyof { a: number, b:string}
=>"a" | "b"
keyof { [x: string] : number }
=>string | number
keyof { [x: number] : number }
=>number
keyof {}[]
=>"toString" | "pop" |...| number
keyof { a: number, b:string } | { a: number, c:string}
=>"a"
keyof { a: number, b:string } & { a: number, c:string}
=>"a" | "b" | "c"
What about symbols?
private
andprotected
A piece of trivia, accessing private and protected properties are allowed for index access today
ts type T = C["x"]; // OK or not?
nameof
in C# does not do this eitherkeyof T
is public-only known properties, private and protected properties are not includedT[K]
K
parameter iff it is constraint to thekeyof T
Putting them together
Index access unification
T[K]
for a type should be the same as that for an expressiont[k]
.Issues
Variance
Consider this:
Keyof T
should be a union for types of all properties for read, but an intersection for write.Where we can land:
Note about
readonly
:setPropoerty
.readonly keyof T
andreadwrite keyof T
.The text was updated successfully, but these errors were encountered: