-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Improve inference for context sensitive functions within reverse mapped types #54029
New issue
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
Open
Andarist
wants to merge
19
commits into
microsoft:main
Choose a base branch
from
Andarist:intra-inference-in-reverse-mapped-types
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
e90edb3
Improve inference for context sensitive functions within reverse mapp…
Andarist 557cd99
use more specialized `Debug.assertNode`
Andarist 4774f8f
Merge remote-tracking branch 'origin/main' into intra-inference-in-re…
Andarist b7eae05
check freshness of the source type before attempting to infer from in…
Andarist a0804b5
get value declaration from the passed in argument
Andarist 57b54c9
Ignore `valueDeclaration` in `getDeclarationModifierFlagsFromSymbol` …
Andarist fae5ae6
Add an extra test case for array literals used as values of object re…
Andarist 34d675a
Merge remote-tracking branch 'origin/main' into intra-inference-in-re…
Andarist a8e8a24
Merge remote-tracking branch 'origin/main' into intra-inference-in-re…
Andarist d71dcd2
Merge remote-tracking branch 'origin/main' into intra-inference-in-re…
Andarist 5b33846
refactor
Andarist cca600f
add comments
Andarist e7e9125
fmt
Andarist 5086dfa
fix cache
Andarist b5c6ef4
add extra tests
Andarist 9e748b7
prefer inferring from non-context sensitive type
Andarist f38ceb3
use a different approach to link partial reverse mapped type with the…
Andarist 2724523
Merge remote-tracking branch 'origin/main' into intra-inference-in-re…
Andarist ec14355
Merge remote-tracking branch 'origin/main' into intra-inference-in-re…
Andarist File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
310 changes: 310 additions & 0 deletions
310
tests/baselines/reference/intraExpressionInferencesReverseMappedTypes.errors.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,310 @@ | ||
| intraExpressionInferencesReverseMappedTypes.ts(67,21): error TS18046: 'x' is of type 'unknown'. | ||
| intraExpressionInferencesReverseMappedTypes.ts(71,21): error TS18046: 'x' is of type 'unknown'. | ||
| intraExpressionInferencesReverseMappedTypes.ts(80,21): error TS18046: 'x' is of type 'unknown'. | ||
| intraExpressionInferencesReverseMappedTypes.ts(86,21): error TS18046: 'x' is of type 'unknown'. | ||
| intraExpressionInferencesReverseMappedTypes.ts(95,21): error TS18046: 'x' is of type 'unknown'. | ||
| intraExpressionInferencesReverseMappedTypes.ts(101,21): error TS18046: 'x' is of type 'unknown'. | ||
|
|
||
|
|
||
| ==== intraExpressionInferencesReverseMappedTypes.ts (6 errors) ==== | ||
| // repro cases based on https://github.com/microsoft/TypeScript/issues/53018 | ||
|
|
||
| declare function f<T>( | ||
| arg: { | ||
| [K in keyof T]: { | ||
| produce: (n: string) => T[K]; | ||
| consume: (x: T[K]) => void; | ||
| }; | ||
| } | ||
| ): T; | ||
|
|
||
| const res1 = f({ | ||
| a: { | ||
| produce: (n) => n, | ||
| consume: (x) => x.toLowerCase(), | ||
| }, | ||
| b: { | ||
| produce: (n) => ({ v: n }), | ||
| consume: (x) => x.v.toLowerCase(), | ||
| }, | ||
| }); | ||
|
|
||
| const res2 = f({ | ||
| a: { | ||
| produce: function () { | ||
| return "hello"; | ||
| }, | ||
| consume: (x) => x.toLowerCase(), | ||
| }, | ||
| b: { | ||
| produce: function () { | ||
| return { v: "hello" }; | ||
| }, | ||
| consume: (x) => x.v.toLowerCase(), | ||
| }, | ||
| }); | ||
|
|
||
| const res3 = f({ | ||
| a: { | ||
| produce() { | ||
| return "hello"; | ||
| }, | ||
| consume: (x) => x.toLowerCase(), | ||
| }, | ||
| b: { | ||
| produce() { | ||
| return { v: "hello" }; | ||
| }, | ||
| consume: (x) => x.v.toLowerCase(), | ||
| }, | ||
| }); | ||
|
|
||
| declare function f2<T extends unknown[]>( | ||
| arg: [ | ||
| ...{ | ||
| [K in keyof T]: { | ||
| produce: (n: string) => T[K]; | ||
| consume: (x: T[K]) => void; | ||
| }; | ||
| } | ||
| ] | ||
| ): T; | ||
|
|
||
| const res4 = f2([ | ||
| { | ||
| produce: (n) => n, | ||
| consume: (x) => x.toLowerCase(), | ||
| ~ | ||
| !!! error TS18046: 'x' is of type 'unknown'. | ||
| }, | ||
| { | ||
| produce: (n) => ({ v: n }), | ||
| consume: (x) => x.v.toLowerCase(), | ||
| ~ | ||
| !!! error TS18046: 'x' is of type 'unknown'. | ||
| }, | ||
| ]); | ||
|
|
||
| const res5 = f2([ | ||
| { | ||
| produce: function () { | ||
| return "hello"; | ||
| }, | ||
| consume: (x) => x.toLowerCase(), | ||
| ~ | ||
| !!! error TS18046: 'x' is of type 'unknown'. | ||
| }, | ||
| { | ||
| produce: function () { | ||
| return { v: "hello" }; | ||
| }, | ||
| consume: (x) => x.v.toLowerCase(), | ||
| ~ | ||
| !!! error TS18046: 'x' is of type 'unknown'. | ||
| }, | ||
| ]); | ||
|
|
||
| const res6 = f2([ | ||
| { | ||
| produce() { | ||
| return "hello"; | ||
| }, | ||
| consume: (x) => x.toLowerCase(), | ||
| ~ | ||
| !!! error TS18046: 'x' is of type 'unknown'. | ||
| }, | ||
| { | ||
| produce() { | ||
| return { v: "hello" }; | ||
| }, | ||
| consume: (x) => x.v.toLowerCase(), | ||
| ~ | ||
| !!! error TS18046: 'x' is of type 'unknown'. | ||
| }, | ||
| ]); | ||
|
|
||
| declare function f3<T>( | ||
| arg: { | ||
| [K in keyof T]: { | ||
| other: number, | ||
| produce: (n: string) => T[K]; | ||
| consume: (x: T[K]) => void; | ||
| }; | ||
| } | ||
| ): T; | ||
|
|
||
| const res7 = f3({ | ||
| a: { | ||
| other: 42, | ||
| produce: (n) => n, | ||
| consume: (x) => x.toLowerCase(), | ||
| }, | ||
| b: { | ||
| other: 100, | ||
| produce: (n) => ({ v: n }), | ||
| consume: (x) => x.v.toLowerCase(), | ||
| }, | ||
| }); | ||
|
|
||
| declare function f4<T>( | ||
| arg: { | ||
| [K in keyof T]: [ | ||
| (n: string) => T[K], | ||
| (x: T[K]) => void | ||
| ]; | ||
| } | ||
| ): T; | ||
|
|
||
| const res8 = f4({ | ||
| a: [ | ||
| (n) => n, | ||
| (x) => x.toLowerCase(), | ||
| ], | ||
| b: [ | ||
| (n) => ({ v: n }), | ||
| (x) => x.v.toLowerCase(), | ||
| ], | ||
| }); | ||
|
|
||
| declare function f5<T1, T2>( | ||
| arg: { | ||
| [K in keyof T1]: { | ||
| produce1: (n: string) => T1[K]; | ||
| consume1: (x: T1[K]) => void; | ||
| }; | ||
| } & { | ||
| [K in keyof T2]: { | ||
| produce2: (n: string) => T2[K]; | ||
| consume2: (x: T2[K]) => void; | ||
| }; | ||
| }, | ||
| ): [T1, T2]; | ||
|
|
||
| const res9 = f5({ | ||
| a: { | ||
| produce1: (n) => n, | ||
| consume1: (x) => x.toLowerCase(), | ||
| produce2: (n) => [n], | ||
| consume2: (x) => x[0].toLowerCase(), | ||
| }, | ||
| b: { | ||
| produce1: (n) => ({ v: n }), | ||
| consume1: (x) => x.v.toLowerCase(), | ||
| produce2: (n) => ({ v: [n] }), | ||
| consume2: (x) => x.v[0].toLowerCase(), | ||
| }, | ||
| }); | ||
|
|
||
| declare function f6<T>(arg: { | ||
| [K in keyof T]: () => { | ||
| produce: (n: string) => T[K]; | ||
| consume: (x: T[K]) => void; | ||
| }; | ||
| }): T; | ||
|
|
||
| const res10 = f6({ | ||
| a() { | ||
| return { | ||
| produce: (n) => n, | ||
| consume: (x) => x.toLowerCase(), | ||
| }; | ||
| }, | ||
| b() { | ||
| return { | ||
| produce: (n) => ({ v: n }), | ||
| consume: (x) => x.v.toLowerCase(), | ||
| }; | ||
| }, | ||
| }); | ||
|
|
||
| const res11 = f6({ | ||
| a: () => { | ||
| return { | ||
| produce: (n) => n, | ||
| consume: (x) => x.toLowerCase(), | ||
| }; | ||
| }, | ||
| b: () => { | ||
| return { | ||
| produce: (n) => ({ v: n }), | ||
| consume: (x) => x.v.toLowerCase(), | ||
| }; | ||
| }, | ||
| }); | ||
|
|
||
| declare function f7<T>(arg: { | ||
| [K in keyof T]: (arg: boolean) => { | ||
| produce: (n: string) => T[K]; | ||
| consume: (x: T[K]) => void; | ||
| }; | ||
| }): T; | ||
|
|
||
| const res12 = f7({ | ||
| a(arg) { | ||
| return { | ||
| produce: (n) => n, | ||
| consume: (x) => x.toLowerCase(), | ||
| }; | ||
| }, | ||
| b(arg) { | ||
| return { | ||
| produce: (n) => ({ v: n }), | ||
| consume: (x) => x.v.toLowerCase(), | ||
| }; | ||
| }, | ||
| }); | ||
|
|
||
| const res13 = f7({ | ||
| a: (arg) => { | ||
| return { | ||
| produce: (n) => n, | ||
| consume: (x) => x.toLowerCase(), | ||
| }; | ||
| }, | ||
| b: (arg) => { | ||
| return { | ||
| produce: (n) => ({ v: n }), | ||
| consume: (x) => x.v.toLowerCase(), | ||
| }; | ||
| }, | ||
| }); | ||
|
|
||
| declare function f8<T>(arg: { | ||
| [K in keyof T]: () => [(n: string) => T[K], (x: T[K]) => void]; | ||
| }): T; | ||
|
|
||
| const res14 = f8({ | ||
| a() { | ||
| return [(n) => n, (x) => x.toLowerCase()]; | ||
| }, | ||
| b() { | ||
| return [(n) => ({ v: n }), (x) => x.v.toLowerCase()]; | ||
| }, | ||
| }); | ||
|
|
||
| declare function f9<T1, T2>( | ||
| arg: { | ||
| [K in keyof T1]: { | ||
| produce: (n: string) => [T1[K], any]; | ||
| consume: (x: T1[K]) => void; | ||
| }; | ||
| } & { | ||
| a: { | ||
| produce: (n: string) => [any, T2]; | ||
| consume2: (x: T2) => void; | ||
| }; | ||
| }, | ||
| ): [T1, T2]; | ||
|
|
||
| const res15 = f9({ | ||
| a: { | ||
| produce: (n) => [n, [n]], | ||
| consume2: (x) => x[0].toLowerCase(), | ||
| consume: (x) => x.toLowerCase(), | ||
| }, | ||
| b: { | ||
| produce: (n) => [{ v: n }, null], | ||
| consume: (x) => x.v.toLowerCase(), | ||
| }, | ||
| }); | ||
|
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since I added
.valueDeclarationto reverse mapped properties, we need to ignore this here - otherwise, we break stripping ofreadonlymodifiers that is required by #12589There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Basically, this change just maintains compatibility with the old behavior as
0was always returned from here in the implicit case ofcheckFlags & CheckFlags.ReverseMapped