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
Libraries like Ember, Backbone, and Immutable all use string literals to access properties.
Idea keysof T: a "keys query", which returns a union of literal types for each property in T.
Following this, you could have T[K] which, when K is a literal type, returns the type of a property with the name K on T.
functionget<T,KextendskeysofT>(obj: T,key: K): T[K]{obj[key];}// Returns 'string'get({name: "Daniel"},"name");// Error: '{ name: string }' has no property named 'nmae'.get({name: "Daniel"},"nmae");
Question: Why doesn't key just have the type keysof T?
Because if T was something like { name: string; age: number }, then keysof T would be "name" | "age".
Even if you pass in "name" for key, the type system keeps the type as "name" | "age".
That means the return type will be T["name" | "number"].
In reality, you want to grab the specific literal "name" to do a query.
We could do the "right thing", but that'd require an implicit type parameter, which would get complicated.
Caveats: any object contxtually typed with a string index signature will have keysof effectively return string on it.
The text was updated successfully, but these errors were encountered:
Always Infer Literal Types (#6554, #10676)
Basics
Based on complaints we've seen in the past about how literals don't "stick" for
const
Idea: the literal
1
should always start out with the type1
.Every literal type has a base primitive type.
Changes to Function Return Type Inference
Changes to Type Argument Inference
For a type parameter
T
, a literal type argument will be inferred forT
ifT
is inferred from "top-level" occurrences (allows widening to take place later on).T
has no primitive/literal constraint (user wanted it).T
does not occur at the "top-level" in the return type (more complicated reasons).Substitute Value of
super()
Forthis
(#7574, #10762)Summary:
👍
@mhegazy notes:
this
#7574this
cannot be used beforesuper
is called.new.target
e.g. extendingArray
andError
this
in arrow functionskeysof
Operator (#1295, #10425)Libraries like Ember, Backbone, and Immutable all use string literals to access properties.
Idea
keysof T
: a "keys query", which returns a union of literal types for each property inT
.Following this, you could have
T[K]
which, whenK
is a literal type, returns the type of a property with the nameK
onT
.key
just have the typekeysof T
?T
was something like{ name: string; age: number }
, thenkeysof T
would be"name" | "age"
.Even if you pass in
"name"
forkey
, the type system keeps the type as"name" | "age"
.That means the return type will be
T["name" | "number"]
.In reality, you want to grab the specific literal
"name"
to do a query.Caveats: any object contxtually typed with a
string
index signature will havekeysof
effectively returnstring
on it.The text was updated successfully, but these errors were encountered: