-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Fixed an issue with string mappings over generic intersections not being deferred in conditional types #55856
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
Merged
sandersn
merged 5 commits into
microsoft:main
from
Andarist:fix/string-mapping-deferral
Oct 26, 2023
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5c9a5a7
Fixed an issue with string mappings over generic intersections not be…
Andarist 9f56c7f
add extra test cases
Andarist f063271
Fixed an extra test case
Andarist a678738
Simplify the fix
Andarist 5d29c23
remove redundant passthrough function
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
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
68 changes: 68 additions & 0 deletions
68
tests/baselines/reference/stringMappingDeferralInConditionalTypes.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,68 @@ | ||
| //// [tests/cases/conformance/types/literal/stringMappingDeferralInConditionalTypes.ts] //// | ||
|
|
||
| === stringMappingDeferralInConditionalTypes.ts === | ||
| // https://github.com/microsoft/TypeScript/issues/55847 | ||
|
|
||
| type A<S> = Lowercase<S & string> extends "foo" ? 1 : 0; | ||
| >A : Symbol(A, Decl(stringMappingDeferralInConditionalTypes.ts, 0, 0)) | ||
| >S : Symbol(S, Decl(stringMappingDeferralInConditionalTypes.ts, 2, 7)) | ||
| >Lowercase : Symbol(Lowercase, Decl(lib.es5.d.ts, --, --)) | ||
| >S : Symbol(S, Decl(stringMappingDeferralInConditionalTypes.ts, 2, 7)) | ||
|
|
||
| let x1: A<"foo"> = 1; // ok | ||
| >x1 : Symbol(x1, Decl(stringMappingDeferralInConditionalTypes.ts, 3, 3)) | ||
| >A : Symbol(A, Decl(stringMappingDeferralInConditionalTypes.ts, 0, 0)) | ||
|
|
||
| type B<S> = Lowercase<S & string> extends `f${string}` ? 1 : 0; | ||
| >B : Symbol(B, Decl(stringMappingDeferralInConditionalTypes.ts, 3, 21)) | ||
| >S : Symbol(S, Decl(stringMappingDeferralInConditionalTypes.ts, 5, 7)) | ||
| >Lowercase : Symbol(Lowercase, Decl(lib.es5.d.ts, --, --)) | ||
| >S : Symbol(S, Decl(stringMappingDeferralInConditionalTypes.ts, 5, 7)) | ||
|
|
||
| let x2: B<"foo"> = 1; // ok | ||
| >x2 : Symbol(x2, Decl(stringMappingDeferralInConditionalTypes.ts, 6, 3)) | ||
| >B : Symbol(B, Decl(stringMappingDeferralInConditionalTypes.ts, 3, 21)) | ||
|
|
||
| type C<S> = Capitalize<Lowercase<S & string>> extends "Foo" ? 1 : 0; | ||
| >C : Symbol(C, Decl(stringMappingDeferralInConditionalTypes.ts, 6, 21)) | ||
| >S : Symbol(S, Decl(stringMappingDeferralInConditionalTypes.ts, 8, 7)) | ||
| >Capitalize : Symbol(Capitalize, Decl(lib.es5.d.ts, --, --)) | ||
| >Lowercase : Symbol(Lowercase, Decl(lib.es5.d.ts, --, --)) | ||
| >S : Symbol(S, Decl(stringMappingDeferralInConditionalTypes.ts, 8, 7)) | ||
|
|
||
| let x3: C<"foo"> = 1; // ok | ||
| >x3 : Symbol(x3, Decl(stringMappingDeferralInConditionalTypes.ts, 9, 3)) | ||
| >C : Symbol(C, Decl(stringMappingDeferralInConditionalTypes.ts, 6, 21)) | ||
|
|
||
| type D<S extends string> = Capitalize<Lowercase<S>> extends "Foo" ? 1 : 0; | ||
| >D : Symbol(D, Decl(stringMappingDeferralInConditionalTypes.ts, 9, 21)) | ||
| >S : Symbol(S, Decl(stringMappingDeferralInConditionalTypes.ts, 11, 7)) | ||
| >Capitalize : Symbol(Capitalize, Decl(lib.es5.d.ts, --, --)) | ||
| >Lowercase : Symbol(Lowercase, Decl(lib.es5.d.ts, --, --)) | ||
| >S : Symbol(S, Decl(stringMappingDeferralInConditionalTypes.ts, 11, 7)) | ||
|
|
||
| let x4: D<"foo"> = 1; // ok | ||
| >x4 : Symbol(x4, Decl(stringMappingDeferralInConditionalTypes.ts, 12, 3)) | ||
| >D : Symbol(D, Decl(stringMappingDeferralInConditionalTypes.ts, 9, 21)) | ||
|
|
||
| type E<S> = Lowercase<`f${S & string}` & `${S & string}f`>; | ||
| >E : Symbol(E, Decl(stringMappingDeferralInConditionalTypes.ts, 12, 21)) | ||
| >S : Symbol(S, Decl(stringMappingDeferralInConditionalTypes.ts, 14, 7)) | ||
| >Lowercase : Symbol(Lowercase, Decl(lib.es5.d.ts, --, --)) | ||
| >S : Symbol(S, Decl(stringMappingDeferralInConditionalTypes.ts, 14, 7)) | ||
| >S : Symbol(S, Decl(stringMappingDeferralInConditionalTypes.ts, 14, 7)) | ||
|
|
||
| type F = E<""> extends "f" ? 1 : 0; // 1 | ||
| >F : Symbol(F, Decl(stringMappingDeferralInConditionalTypes.ts, 14, 59)) | ||
| >E : Symbol(E, Decl(stringMappingDeferralInConditionalTypes.ts, 12, 21)) | ||
|
|
||
| type G<S> = E<S> extends "f" ? 1 : 0; | ||
| >G : Symbol(G, Decl(stringMappingDeferralInConditionalTypes.ts, 15, 35)) | ||
| >S : Symbol(S, Decl(stringMappingDeferralInConditionalTypes.ts, 16, 7)) | ||
| >E : Symbol(E, Decl(stringMappingDeferralInConditionalTypes.ts, 12, 21)) | ||
| >S : Symbol(S, Decl(stringMappingDeferralInConditionalTypes.ts, 16, 7)) | ||
|
|
||
| let x5: G<""> = 1; // ok | ||
| >x5 : Symbol(x5, Decl(stringMappingDeferralInConditionalTypes.ts, 17, 3)) | ||
| >G : Symbol(G, Decl(stringMappingDeferralInConditionalTypes.ts, 15, 35)) | ||
|
|
46 changes: 46 additions & 0 deletions
46
tests/baselines/reference/stringMappingDeferralInConditionalTypes.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,46 @@ | ||
| //// [tests/cases/conformance/types/literal/stringMappingDeferralInConditionalTypes.ts] //// | ||
|
|
||
| === stringMappingDeferralInConditionalTypes.ts === | ||
| // https://github.com/microsoft/TypeScript/issues/55847 | ||
|
|
||
| type A<S> = Lowercase<S & string> extends "foo" ? 1 : 0; | ||
| >A : A<S> | ||
|
|
||
| let x1: A<"foo"> = 1; // ok | ||
| >x1 : 1 | ||
| >1 : 1 | ||
|
|
||
| type B<S> = Lowercase<S & string> extends `f${string}` ? 1 : 0; | ||
| >B : B<S> | ||
|
|
||
| let x2: B<"foo"> = 1; // ok | ||
| >x2 : 1 | ||
| >1 : 1 | ||
|
|
||
| type C<S> = Capitalize<Lowercase<S & string>> extends "Foo" ? 1 : 0; | ||
| >C : C<S> | ||
|
|
||
| let x3: C<"foo"> = 1; // ok | ||
| >x3 : 1 | ||
| >1 : 1 | ||
|
|
||
| type D<S extends string> = Capitalize<Lowercase<S>> extends "Foo" ? 1 : 0; | ||
| >D : D<S> | ||
|
|
||
| let x4: D<"foo"> = 1; // ok | ||
| >x4 : 1 | ||
| >1 : 1 | ||
|
|
||
| type E<S> = Lowercase<`f${S & string}` & `${S & string}f`>; | ||
| >E : Lowercase<`f${S & string}` & `${S & string}f`> | ||
|
|
||
| type F = E<""> extends "f" ? 1 : 0; // 1 | ||
| >F : 1 | ||
|
|
||
| type G<S> = E<S> extends "f" ? 1 : 0; | ||
| >G : G<S> | ||
|
|
||
| let x5: G<""> = 1; // ok | ||
| >x5 : 1 | ||
| >1 : 1 | ||
|
|
21 changes: 21 additions & 0 deletions
21
tests/cases/conformance/types/literal/stringMappingDeferralInConditionalTypes.ts
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,21 @@ | ||
| // @strict: true | ||
| // @noEmit: true | ||
|
|
||
| // https://github.com/microsoft/TypeScript/issues/55847 | ||
|
|
||
| type A<S> = Lowercase<S & string> extends "foo" ? 1 : 0; | ||
| let x1: A<"foo"> = 1; // ok | ||
|
|
||
| type B<S> = Lowercase<S & string> extends `f${string}` ? 1 : 0; | ||
| let x2: B<"foo"> = 1; // ok | ||
|
|
||
| type C<S> = Capitalize<Lowercase<S & string>> extends "Foo" ? 1 : 0; | ||
| let x3: C<"foo"> = 1; // ok | ||
|
|
||
| type D<S extends string> = Capitalize<Lowercase<S>> extends "Foo" ? 1 : 0; | ||
| let x4: D<"foo"> = 1; // ok | ||
|
|
||
| type E<S> = Lowercase<`f${S & string}` & `${S & string}f`>; | ||
| type F = E<""> extends "f" ? 1 : 0; // 1 | ||
| type G<S> = E<S> extends "f" ? 1 : 0; | ||
| let x5: G<""> = 1; // ok |
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.
I think this entire fix can be simplified to simply adding a check that the type isn't generic:
Generic types should never be classified as placeholders since upon instantiation they may become something completely different.
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.
Oh, that's cool! Thanks for the tip. I was worried that
addSpanswould fail if I did something like this. It seems that (somewhat confusingly)isGenericIndexTypealready returnstruefor an intersection like this soaddSpansis covered.