-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Properly handle non-generic string mapping types in unions and intersections #57197
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
Merged
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
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,97 @@ | ||
//// [tests/cases/conformance/types/literal/stringMappingReduction.ts] //// | ||
|
||
=== stringMappingReduction.ts === | ||
type T00 = "prop" | `p${Lowercase<string>}p`; // `p${Lowercase<string>}p` | ||
>T00 : Symbol(T00, Decl(stringMappingReduction.ts, 0, 0)) | ||
>Lowercase : Symbol(Lowercase, Decl(lib.es5.d.ts, --, --)) | ||
|
||
type T01 = "prop" | Lowercase<string>; // Lowercase<string> | ||
>T01 : Symbol(T01, Decl(stringMappingReduction.ts, 0, 45)) | ||
>Lowercase : Symbol(Lowercase, Decl(lib.es5.d.ts, --, --)) | ||
|
||
type T02 = "PROP" | Lowercase<string>; // "PROP" | Lowercase<string> | ||
>T02 : Symbol(T02, Decl(stringMappingReduction.ts, 1, 38)) | ||
>Lowercase : Symbol(Lowercase, Decl(lib.es5.d.ts, --, --)) | ||
|
||
type T10 = "prop" & `p${Lowercase<string>}p`; // "prop" | ||
>T10 : Symbol(T10, Decl(stringMappingReduction.ts, 2, 38)) | ||
>Lowercase : Symbol(Lowercase, Decl(lib.es5.d.ts, --, --)) | ||
|
||
type T11 = "prop" & Lowercase<string>; // "prop" | ||
>T11 : Symbol(T11, Decl(stringMappingReduction.ts, 4, 45)) | ||
>Lowercase : Symbol(Lowercase, Decl(lib.es5.d.ts, --, --)) | ||
|
||
type T12 = "PROP" & Lowercase<string>; // never | ||
>T12 : Symbol(T12, Decl(stringMappingReduction.ts, 5, 38)) | ||
>Lowercase : Symbol(Lowercase, Decl(lib.es5.d.ts, --, --)) | ||
|
||
type T20 = "prop" | Capitalize<string>; // "prop" | Capitalize<string> | ||
>T20 : Symbol(T20, Decl(stringMappingReduction.ts, 6, 38)) | ||
>Capitalize : Symbol(Capitalize, Decl(lib.es5.d.ts, --, --)) | ||
|
||
type T21 = "Prop" | Capitalize<string>; // Capitalize<string> | ||
>T21 : Symbol(T21, Decl(stringMappingReduction.ts, 8, 39)) | ||
>Capitalize : Symbol(Capitalize, Decl(lib.es5.d.ts, --, --)) | ||
|
||
type T22 = "PROP" | Capitalize<string>; // Capitalize<string> | ||
>T22 : Symbol(T22, Decl(stringMappingReduction.ts, 9, 39)) | ||
>Capitalize : Symbol(Capitalize, Decl(lib.es5.d.ts, --, --)) | ||
|
||
type T30 = "prop" & Capitalize<string>; // never | ||
>T30 : Symbol(T30, Decl(stringMappingReduction.ts, 10, 39)) | ||
>Capitalize : Symbol(Capitalize, Decl(lib.es5.d.ts, --, --)) | ||
|
||
type T31 = "Prop" & Capitalize<string>; // "Prop" | ||
>T31 : Symbol(T31, Decl(stringMappingReduction.ts, 12, 39)) | ||
>Capitalize : Symbol(Capitalize, Decl(lib.es5.d.ts, --, --)) | ||
|
||
type T32 = "PROP" & Capitalize<string>; // "PROP" | ||
>T32 : Symbol(T32, Decl(stringMappingReduction.ts, 13, 39)) | ||
>Capitalize : Symbol(Capitalize, Decl(lib.es5.d.ts, --, --)) | ||
|
||
// Repro from #57117 | ||
|
||
type EMap = { event: {} } | ||
>EMap : Symbol(EMap, Decl(stringMappingReduction.ts, 14, 39)) | ||
>event : Symbol(event, Decl(stringMappingReduction.ts, 18, 13)) | ||
|
||
type Keys = keyof EMap | ||
>Keys : Symbol(Keys, Decl(stringMappingReduction.ts, 18, 25)) | ||
>EMap : Symbol(EMap, Decl(stringMappingReduction.ts, 14, 39)) | ||
|
||
type EPlusFallback<C> = C extends Keys ? EMap[C] : "unrecognised event"; | ||
>EPlusFallback : Symbol(EPlusFallback, Decl(stringMappingReduction.ts, 19, 22)) | ||
>C : Symbol(C, Decl(stringMappingReduction.ts, 20, 19)) | ||
>C : Symbol(C, Decl(stringMappingReduction.ts, 20, 19)) | ||
>Keys : Symbol(Keys, Decl(stringMappingReduction.ts, 18, 25)) | ||
>EMap : Symbol(EMap, Decl(stringMappingReduction.ts, 14, 39)) | ||
>C : Symbol(C, Decl(stringMappingReduction.ts, 20, 19)) | ||
|
||
type VirtualEvent<T extends string> = { bivarianceHack(event: EPlusFallback<Lowercase<T>>): any; }['bivarianceHack']; | ||
>VirtualEvent : Symbol(VirtualEvent, Decl(stringMappingReduction.ts, 20, 72)) | ||
>T : Symbol(T, Decl(stringMappingReduction.ts, 21, 18)) | ||
>bivarianceHack : Symbol(bivarianceHack, Decl(stringMappingReduction.ts, 21, 39)) | ||
>event : Symbol(event, Decl(stringMappingReduction.ts, 21, 55)) | ||
>EPlusFallback : Symbol(EPlusFallback, Decl(stringMappingReduction.ts, 19, 22)) | ||
>Lowercase : Symbol(Lowercase, Decl(lib.es5.d.ts, --, --)) | ||
>T : Symbol(T, Decl(stringMappingReduction.ts, 21, 18)) | ||
|
||
declare const _virtualOn: (eventQrl: VirtualEvent<Keys>) => void; | ||
>_virtualOn : Symbol(_virtualOn, Decl(stringMappingReduction.ts, 22, 13)) | ||
>eventQrl : Symbol(eventQrl, Decl(stringMappingReduction.ts, 22, 27)) | ||
>VirtualEvent : Symbol(VirtualEvent, Decl(stringMappingReduction.ts, 20, 72)) | ||
>Keys : Symbol(Keys, Decl(stringMappingReduction.ts, 18, 25)) | ||
|
||
export const virtualOn = <T extends string>(eventQrl: VirtualEvent<T>) => { | ||
>virtualOn : Symbol(virtualOn, Decl(stringMappingReduction.ts, 23, 12)) | ||
>T : Symbol(T, Decl(stringMappingReduction.ts, 23, 26)) | ||
>eventQrl : Symbol(eventQrl, Decl(stringMappingReduction.ts, 23, 44)) | ||
>VirtualEvent : Symbol(VirtualEvent, Decl(stringMappingReduction.ts, 20, 72)) | ||
>T : Symbol(T, Decl(stringMappingReduction.ts, 23, 26)) | ||
|
||
_virtualOn(eventQrl); | ||
>_virtualOn : Symbol(_virtualOn, Decl(stringMappingReduction.ts, 22, 13)) | ||
>eventQrl : Symbol(eventQrl, Decl(stringMappingReduction.ts, 23, 44)) | ||
|
||
}; | ||
|
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,72 @@ | ||
//// [tests/cases/conformance/types/literal/stringMappingReduction.ts] //// | ||
|
||
=== stringMappingReduction.ts === | ||
type T00 = "prop" | `p${Lowercase<string>}p`; // `p${Lowercase<string>}p` | ||
>T00 : `p${Lowercase<string>}p` | ||
|
||
type T01 = "prop" | Lowercase<string>; // Lowercase<string> | ||
>T01 : Lowercase<string> | ||
|
||
type T02 = "PROP" | Lowercase<string>; // "PROP" | Lowercase<string> | ||
>T02 : Lowercase<string> | "PROP" | ||
|
||
type T10 = "prop" & `p${Lowercase<string>}p`; // "prop" | ||
>T10 : "prop" | ||
|
||
type T11 = "prop" & Lowercase<string>; // "prop" | ||
>T11 : "prop" | ||
|
||
type T12 = "PROP" & Lowercase<string>; // never | ||
>T12 : never | ||
|
||
type T20 = "prop" | Capitalize<string>; // "prop" | Capitalize<string> | ||
>T20 : "prop" | Capitalize<string> | ||
|
||
type T21 = "Prop" | Capitalize<string>; // Capitalize<string> | ||
>T21 : Capitalize<string> | ||
|
||
type T22 = "PROP" | Capitalize<string>; // Capitalize<string> | ||
>T22 : Capitalize<string> | ||
|
||
type T30 = "prop" & Capitalize<string>; // never | ||
>T30 : never | ||
|
||
type T31 = "Prop" & Capitalize<string>; // "Prop" | ||
>T31 : "Prop" | ||
|
||
type T32 = "PROP" & Capitalize<string>; // "PROP" | ||
>T32 : "PROP" | ||
|
||
// Repro from #57117 | ||
|
||
type EMap = { event: {} } | ||
>EMap : { event: {}; } | ||
>event : {} | ||
|
||
type Keys = keyof EMap | ||
>Keys : "event" | ||
|
||
type EPlusFallback<C> = C extends Keys ? EMap[C] : "unrecognised event"; | ||
>EPlusFallback : EPlusFallback<C> | ||
|
||
type VirtualEvent<T extends string> = { bivarianceHack(event: EPlusFallback<Lowercase<T>>): any; }['bivarianceHack']; | ||
>VirtualEvent : (event: EPlusFallback<Lowercase<T>>) => any | ||
>bivarianceHack : (event: EPlusFallback<Lowercase<T>>) => any | ||
>event : EPlusFallback<Lowercase<T>> | ||
|
||
declare const _virtualOn: (eventQrl: VirtualEvent<Keys>) => void; | ||
>_virtualOn : (eventQrl: VirtualEvent<Keys>) => void | ||
>eventQrl : (event: {}) => any | ||
|
||
export const virtualOn = <T extends string>(eventQrl: VirtualEvent<T>) => { | ||
>virtualOn : <T extends string>(eventQrl: VirtualEvent<T>) => void | ||
><T extends string>(eventQrl: VirtualEvent<T>) => { _virtualOn(eventQrl);} : <T extends string>(eventQrl: VirtualEvent<T>) => void | ||
>eventQrl : (event: EPlusFallback<Lowercase<T>>) => any | ||
|
||
_virtualOn(eventQrl); | ||
>_virtualOn(eventQrl) : void | ||
>_virtualOn : (eventQrl: (event: {}) => any) => void | ||
>eventQrl : (event: EPlusFallback<Lowercase<T>>) => any | ||
|
||
}; | ||
|
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
29 changes: 29 additions & 0 deletions
29
tests/cases/conformance/types/literal/stringMappingReduction.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,29 @@ | ||
// @strict: true | ||
// @noEmit: true | ||
|
||
type T00 = "prop" | `p${Lowercase<string>}p`; // `p${Lowercase<string>}p` | ||
type T01 = "prop" | Lowercase<string>; // Lowercase<string> | ||
type T02 = "PROP" | Lowercase<string>; // "PROP" | Lowercase<string> | ||
|
||
type T10 = "prop" & `p${Lowercase<string>}p`; // "prop" | ||
type T11 = "prop" & Lowercase<string>; // "prop" | ||
type T12 = "PROP" & Lowercase<string>; // never | ||
|
||
type T20 = "prop" | Capitalize<string>; // "prop" | Capitalize<string> | ||
type T21 = "Prop" | Capitalize<string>; // Capitalize<string> | ||
type T22 = "PROP" | Capitalize<string>; // Capitalize<string> | ||
|
||
type T30 = "prop" & Capitalize<string>; // never | ||
type T31 = "Prop" & Capitalize<string>; // "Prop" | ||
type T32 = "PROP" & Capitalize<string>; // "PROP" | ||
|
||
// Repro from #57117 | ||
|
||
type EMap = { event: {} } | ||
type Keys = keyof EMap | ||
type EPlusFallback<C> = C extends Keys ? EMap[C] : "unrecognised event"; | ||
type VirtualEvent<T extends string> = { bivarianceHack(event: EPlusFallback<Lowercase<T>>): any; }['bivarianceHack']; | ||
declare const _virtualOn: (eventQrl: VirtualEvent<Keys>) => void; | ||
export const virtualOn = <T extends string>(eventQrl: VirtualEvent<T>) => { | ||
_virtualOn(eventQrl); | ||
}; |
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 is kind of funky - I'd just directly make this
IncludesConstrainedTypeVariable
.