Uh oh!
There was an error while loading. Please reload this page.
Improve soundness of indexed access types - #30769
Conversation
Has the behaviour that was added in #27490 been taken out? The following function did error, but not under the new rules. functionfun<Uextends{a: T},T,Kextends'a'>(u: U,k: K,shambles: T): U[K]{u[k]=shambles;// was errorreturnu[k];}constresult: number=fun<{a: number},unknown,'a'>({a: 42},'a','a string');I think the reason that the test-cases from #27470 didn't regress is because the restrictive instantiation has been subsequently added to conditional types, which catches the error too. |
ahejlsberg
commented
Apr 6, 2019
Yes, but that behavior wasn't right. For an indexed access The real (and orthogonal) issue in your example is that we treat an assignment to What has changed with this PR is that for a It's all rather involved! |
ahejlsberg
commented
Apr 6, 2019
@weswigham Can you take a look at why this is failing on |
weswigham
commented
Apr 6, 2019
Looks like we're pulling out some unexpected index signatures from the target or something? The bug should repro in the test harness, will just need to copy the relevant types into a test file. |
ahejlsberg
commented
Apr 6, 2019
@weswigham It's actually identifying an unsafe coercion. It used to be that The easy fix is to explicitly cast the array parameter to |
ahejlsberg
commented
Apr 6, 2019
@typescript-bot test this |
Heya @ahejlsberg, I've started to run the extended test suite on this PR at e1fd5e5. You can monitor the build here. It should now contribute to this PR's status checks. |
ahejlsberg
commented
Apr 6, 2019
@typescript-bot run dt |
Heya @ahejlsberg, I've started to run the Definitely Typed test suite on this PR at e1fd5e5. You can monitor the build here. It should now contribute to this PR's status checks. |
jack-williams
commented
Apr 6, 2019
Thanks for the detailed write-up @ahejlsberg.
True, though did you not add behaviour to prevent pulling down |
ahejlsberg
commented
Apr 8, 2019
With latest commits there are three RWC projects with new errors. The errors in two of the projects are minor and identify unsoundness that we previously didn't catch. The last project, |
ahejlsberg
commented
Apr 8, 2019
@typescript-bot test this |
quixot1c
commented
Jan 11, 2022
@ahejlsberg If "a type argument for |
ahejlsberg
commented
Jan 11, 2022
An indexed access |
Could the errors in this test case be overly strict? This Shouldn't TypeScript assume the user is reading the function parameters and trust them not to call Conversely, the user may have a special need for writing a writable any typed function like |
With this PR we improve soundness of indexed access types in a number of ways:
T[K]occurs on the source side of a type relationship, it resolves to a union type of the properties selected byT[K], but when it occurs on the target side of a type relationship, it now resolves to an intersection type of the properties selected byT[K]. Previously, the target side would resolve to a union type as well, which is unsound.Twith a constraintC, when an indexed accessT[K]occurs on the target side of a type relationship, index signatures inCare now ignored. This is because a type argument forTisn't actually required to have an index signature, it is just required to have properties with matching types.{ [key: string]: number }is no longer related to a mapped type{ [P in K]: number }, whereKis a type variable. This is consistent with a string index signature in the source not matching actual properties in the target.TandK extends 'a' | 'b', the types{ a: T, b: T }[K]andTare now considered related where previously they weren't.Some examples:
Previously, none of the above errors were reported.
Fixes#27895.
Fixes#30603.