-
Notifications
You must be signed in to change notification settings - Fork 12.8k
When calculating inferred infer type constraints, replace conditionals… #48648
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
Closed
weswigham
wants to merge
1
commit into
microsoft:main
from
weswigham:infer-implied-constraint-no-exec
Closed
Changes from all commits
Commits
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,7 @@ type KeysWithoutStringIndex<T> = | |
|
||
// Only "foo" | "bar" as expected, [string] index signature removed | ||
type test = KeysWithoutStringIndex<{ [index: string]: string; foo: string; bar: 'baz' }> | ||
>test : never | ||
>test : "foo" | "bar" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fun fact - this test already showed the issue, we just weren't looking for it in this test. (Instead we were looking for a constraint error on a different line.) |
||
>index : string | ||
>foo : string | ||
>bar : "baz" | ||
|
19 changes: 19 additions & 0 deletions
19
tests/baselines/reference/inferImpliedConstraintNotExecuted.js
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,19 @@ | ||
//// [inferImpliedConstraintNotExecuted.ts] | ||
type KnownKeys<T> = { | ||
[K in keyof T]: string extends K ? never : number extends K ? never : K | ||
} extends { [_ in keyof T]: infer U } ? U : never; | ||
|
||
|
||
interface HasStringKeys { | ||
[s: string]: any; | ||
} | ||
|
||
interface ThingWithKeys extends HasStringKeys { | ||
foo: unknown; | ||
bar: unknown; | ||
} | ||
|
||
const demo: KnownKeys<ThingWithKeys> = 'foo'; | ||
|
||
//// [inferImpliedConstraintNotExecuted.js] | ||
var demo = 'foo'; |
42 changes: 42 additions & 0 deletions
42
tests/baselines/reference/inferImpliedConstraintNotExecuted.symbols
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,42 @@ | ||
=== tests/cases/compiler/inferImpliedConstraintNotExecuted.ts === | ||
type KnownKeys<T> = { | ||
>KnownKeys : Symbol(KnownKeys, Decl(inferImpliedConstraintNotExecuted.ts, 0, 0)) | ||
>T : Symbol(T, Decl(inferImpliedConstraintNotExecuted.ts, 0, 15)) | ||
|
||
[K in keyof T]: string extends K ? never : number extends K ? never : K | ||
>K : Symbol(K, Decl(inferImpliedConstraintNotExecuted.ts, 1, 5)) | ||
>T : Symbol(T, Decl(inferImpliedConstraintNotExecuted.ts, 0, 15)) | ||
>K : Symbol(K, Decl(inferImpliedConstraintNotExecuted.ts, 1, 5)) | ||
>K : Symbol(K, Decl(inferImpliedConstraintNotExecuted.ts, 1, 5)) | ||
>K : Symbol(K, Decl(inferImpliedConstraintNotExecuted.ts, 1, 5)) | ||
|
||
} extends { [_ in keyof T]: infer U } ? U : never; | ||
>_ : Symbol(_, Decl(inferImpliedConstraintNotExecuted.ts, 2, 13)) | ||
>T : Symbol(T, Decl(inferImpliedConstraintNotExecuted.ts, 0, 15)) | ||
>U : Symbol(U, Decl(inferImpliedConstraintNotExecuted.ts, 2, 33)) | ||
>U : Symbol(U, Decl(inferImpliedConstraintNotExecuted.ts, 2, 33)) | ||
|
||
|
||
interface HasStringKeys { | ||
>HasStringKeys : Symbol(HasStringKeys, Decl(inferImpliedConstraintNotExecuted.ts, 2, 50)) | ||
|
||
[s: string]: any; | ||
>s : Symbol(s, Decl(inferImpliedConstraintNotExecuted.ts, 6, 5)) | ||
} | ||
|
||
interface ThingWithKeys extends HasStringKeys { | ||
>ThingWithKeys : Symbol(ThingWithKeys, Decl(inferImpliedConstraintNotExecuted.ts, 7, 1)) | ||
>HasStringKeys : Symbol(HasStringKeys, Decl(inferImpliedConstraintNotExecuted.ts, 2, 50)) | ||
|
||
foo: unknown; | ||
>foo : Symbol(ThingWithKeys.foo, Decl(inferImpliedConstraintNotExecuted.ts, 9, 47)) | ||
|
||
bar: unknown; | ||
>bar : Symbol(ThingWithKeys.bar, Decl(inferImpliedConstraintNotExecuted.ts, 10, 17)) | ||
} | ||
|
||
const demo: KnownKeys<ThingWithKeys> = 'foo'; | ||
>demo : Symbol(demo, Decl(inferImpliedConstraintNotExecuted.ts, 14, 5)) | ||
>KnownKeys : Symbol(KnownKeys, Decl(inferImpliedConstraintNotExecuted.ts, 0, 0)) | ||
>ThingWithKeys : Symbol(ThingWithKeys, Decl(inferImpliedConstraintNotExecuted.ts, 7, 1)) | ||
|
25 changes: 25 additions & 0 deletions
25
tests/baselines/reference/inferImpliedConstraintNotExecuted.types
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,25 @@ | ||
=== tests/cases/compiler/inferImpliedConstraintNotExecuted.ts === | ||
type KnownKeys<T> = { | ||
>KnownKeys : KnownKeys<T> | ||
|
||
[K in keyof T]: string extends K ? never : number extends K ? never : K | ||
} extends { [_ in keyof T]: infer U } ? U : never; | ||
|
||
|
||
interface HasStringKeys { | ||
[s: string]: any; | ||
>s : string | ||
} | ||
|
||
interface ThingWithKeys extends HasStringKeys { | ||
foo: unknown; | ||
>foo : unknown | ||
|
||
bar: unknown; | ||
>bar : unknown | ||
} | ||
|
||
const demo: KnownKeys<ThingWithKeys> = 'foo'; | ||
>demo : "foo" | "bar" | ||
>'foo' : "foo" | ||
|
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,15 @@ | ||
type KnownKeys<T> = { | ||
[K in keyof T]: string extends K ? never : number extends K ? never : K | ||
} extends { [_ in keyof T]: infer U } ? U : never; | ||
|
||
|
||
interface HasStringKeys { | ||
[s: string]: any; | ||
} | ||
|
||
interface ThingWithKeys extends HasStringKeys { | ||
foo: unknown; | ||
bar: unknown; | ||
} | ||
|
||
const demo: KnownKeys<ThingWithKeys> = 'foo'; |
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.
This comment block is informative for whomever comes back to this in the future - I looked at two other ways to solve the problem (the conditional in the template type executing differently than the mapped type around it would imply when instantiated in the above fashion), but both ran against other issues in the checker. If one of the other's root issues get fixed, I think they'd be much more elegant constructions to return here than this conditional-constraint-walking logic (ideally the first of the two, since it much more closely matches the structure traversed).