GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||||||||
GeezerVison is showing github.com — Improve soundness of indexed access types by ahejlsberg · Pull Request #30769 · microsoft/TypeScript Conversation
Member
With this PR we improve soundness of indexed access types in a number of ways:
Some examples: function f1(obj: { a: number, b: string }, key: 'a' | 'b') { obj[key] = 1; // Error obj[key] = 'x'; // Error } function f2(obj: { a: number, b: 0 | 1 }, key: 'a' | 'b') { obj[key] = 1; obj[key] = 2; // Error } function f3<T extends { [key: string]: any }>(obj: T) { let foo = obj['foo']; let bar = obj['bar']; obj['foo'] = 123; // Error obj['bar'] = 'x'; // Error } function f4<K extends string>(a: { [P in K]: number }, b: { [key: string]: number }) { a = b; // Error b = a; } Previously, none of the above errors were reported.
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactions
|
There was an error while loading. Please reload this page.
|
Has the behaviour that was added in #27490 been taken out? The following function did error, but not under the new rules. function fun<U extends { a: T }, T, K extends 'a'>(u: U, k: K, shambles: T): U[K] { u[k] = shambles; // was error return u[k]; } const result: 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. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
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! |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Wesley Wigham (@weswigham) Can you take a look at why this is failing on |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
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. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Wesley Wigham (@weswigham) It's actually identifying an unsafe coercion. It used to be that The easy fix is to explicitly cast the array parameter to |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
TypeScript Bot (@typescript-bot) test this |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
Heya Anders Hejlsberg (@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. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
Heya Anders Hejlsberg (@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. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Thanks for the detailed write-up Anders Hejlsberg (@ahejlsberg).
True, though did you not add behaviour to prevent pulling down |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
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, |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
TypeScript Bot (@typescript-bot) test this |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Anders Hejlsberg (@ahejlsberg) If "a type argument for |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
An indexed access |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
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 |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
Function can return wrong values despite type Change in generic behavior between 3.0.1 and >=3.1.1
There was an error while loading. Please reload this page.