We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
This code is patently unsafe:
function f2(obj: { a: number, b: string }, key: 'a' | 'b') { obj[key] = 123; }
We need to get different types for reading and writing an indexed access type.
Also has implications for constraints and assignability
Consider this code:
function foo<T, K extends keyof T>(obj: T, key: K) { obj[key] = 123; }
From a constraint perspective, the correct way to look at a reduction of T[K] is:
T[K]
T extends C
T[A | B]
T[A] | T[B]
(T | U)[K]
T[K] | U[K]
T[K] & U[K]
T[K] = S
T extends { a: string, b: number }
K extends keyof T
K
"a" | "b")
T['a'] | T['b']
T
T extends { [x: string]: any }
T extends { [x: string]: number }
T[""] = 0
T[K] = 0
function print<T>(x: T): void { console.log(x.toString()); } print(null); // A-OK!
{}
Object
unknown
print<T extends {}>
object
string | number | boolean | object | symbol
The text was updated successfully, but these errors were encountered:
No branches or pull requests
Design Meeting Notes 3/29/2019
Safeness of indexed access types (#30603)
This code is patently unsafe:
We need to get different types for reading and writing an indexed access type.
Also has implications for constraints and assignability
Consider this code:
From a constraint perspective, the correct way to look at a reduction of
T[K]
is:T extends C
, then we try to resolveT[K]
using the constraint (possibly recursively)T[A | B]
simplifies toT[A] | T[B]
, which is only correct for reading(T | U)[K]
, the read simplification isT[K] | U[K]
and the write simplification isT[K] & U[K]
T[K] = S
whereT extends { a: string, b: number }
andK extends keyof T
K
("a" | "b")
and then distribute that (T['a'] | T['b']
]) into the narrowest target valueT
case, what do we do?T extends { [x: string]: any }
?T extends { [x: string]: number }
?T[""] = 0
OK or no?T[K] = 0
#30637
T
is{}
and{}
acquires theObject
apparent members.unknown
)print<T extends {}>
which becomes meaningful.{}
coming from broken inference (wow){}
in DT (in files with sufficiently-high version)?unknown
orobject
orstring | number | boolean | object | symbol
){}
, we hardly knew ye" - @rbucktonThe text was updated successfully, but these errors were encountered: