-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Improve isTypeParameterAtTopLevel
to handle substitution types and template literal types
#60643
Open
Andarist
wants to merge
2
commits into
microsoft:main
Choose a base branch
from
Andarist:improve-isTypeParameterAtTopLevel
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.
+342
−4
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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
128 changes: 128 additions & 0 deletions
128
tests/baselines/reference/returnTypeTopLevelInferenceLiteralDontWiden1.symbols
This file contains 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,128 @@ | ||
//// [tests/cases/compiler/returnTypeTopLevelInferenceLiteralDontWiden1.ts] //// | ||
|
||
=== returnTypeTopLevelInferenceLiteralDontWiden1.ts === | ||
declare function A<T>(value: T): T extends string ? T : never; | ||
>A : Symbol(A, Decl(returnTypeTopLevelInferenceLiteralDontWiden1.ts, 0, 0)) | ||
>T : Symbol(T, Decl(returnTypeTopLevelInferenceLiteralDontWiden1.ts, 0, 19)) | ||
>value : Symbol(value, Decl(returnTypeTopLevelInferenceLiteralDontWiden1.ts, 0, 22)) | ||
>T : Symbol(T, Decl(returnTypeTopLevelInferenceLiteralDontWiden1.ts, 0, 19)) | ||
>T : Symbol(T, Decl(returnTypeTopLevelInferenceLiteralDontWiden1.ts, 0, 19)) | ||
>T : Symbol(T, Decl(returnTypeTopLevelInferenceLiteralDontWiden1.ts, 0, 19)) | ||
|
||
const ATest = A("foo"); | ||
>ATest : Symbol(ATest, Decl(returnTypeTopLevelInferenceLiteralDontWiden1.ts, 2, 5)) | ||
>A : Symbol(A, Decl(returnTypeTopLevelInferenceLiteralDontWiden1.ts, 0, 0)) | ||
|
||
declare function B<T>(value: T): `test_${T & string}`; | ||
>B : Symbol(B, Decl(returnTypeTopLevelInferenceLiteralDontWiden1.ts, 2, 23)) | ||
>T : Symbol(T, Decl(returnTypeTopLevelInferenceLiteralDontWiden1.ts, 4, 19)) | ||
>value : Symbol(value, Decl(returnTypeTopLevelInferenceLiteralDontWiden1.ts, 4, 22)) | ||
>T : Symbol(T, Decl(returnTypeTopLevelInferenceLiteralDontWiden1.ts, 4, 19)) | ||
>T : Symbol(T, Decl(returnTypeTopLevelInferenceLiteralDontWiden1.ts, 4, 19)) | ||
|
||
const BTest = B("foo"); | ||
>BTest : Symbol(BTest, Decl(returnTypeTopLevelInferenceLiteralDontWiden1.ts, 6, 5)) | ||
>B : Symbol(B, Decl(returnTypeTopLevelInferenceLiteralDontWiden1.ts, 2, 23)) | ||
|
||
declare function C<T>( | ||
>C : Symbol(C, Decl(returnTypeTopLevelInferenceLiteralDontWiden1.ts, 6, 23)) | ||
>T : Symbol(T, Decl(returnTypeTopLevelInferenceLiteralDontWiden1.ts, 8, 19)) | ||
|
||
value: T, | ||
>value : Symbol(value, Decl(returnTypeTopLevelInferenceLiteralDontWiden1.ts, 8, 22)) | ||
>T : Symbol(T, Decl(returnTypeTopLevelInferenceLiteralDontWiden1.ts, 8, 19)) | ||
|
||
): T extends string | ||
>T : Symbol(T, Decl(returnTypeTopLevelInferenceLiteralDontWiden1.ts, 8, 19)) | ||
|
||
? T | ||
>T : Symbol(T, Decl(returnTypeTopLevelInferenceLiteralDontWiden1.ts, 8, 19)) | ||
|
||
: T extends number | bigint | boolean | null | undefined | ||
>T : Symbol(T, Decl(returnTypeTopLevelInferenceLiteralDontWiden1.ts, 8, 19)) | ||
|
||
? `test_${T}` | ||
>T : Symbol(T, Decl(returnTypeTopLevelInferenceLiteralDontWiden1.ts, 8, 19)) | ||
|
||
: never; | ||
|
||
const CTest = C("foo"); | ||
>CTest : Symbol(CTest, Decl(returnTypeTopLevelInferenceLiteralDontWiden1.ts, 16, 5)) | ||
>C : Symbol(C, Decl(returnTypeTopLevelInferenceLiteralDontWiden1.ts, 6, 23)) | ||
|
||
declare function D<T>(value: T): T extends number ? `test_${T}` : T; | ||
>D : Symbol(D, Decl(returnTypeTopLevelInferenceLiteralDontWiden1.ts, 16, 23)) | ||
>T : Symbol(T, Decl(returnTypeTopLevelInferenceLiteralDontWiden1.ts, 18, 19)) | ||
>value : Symbol(value, Decl(returnTypeTopLevelInferenceLiteralDontWiden1.ts, 18, 22)) | ||
>T : Symbol(T, Decl(returnTypeTopLevelInferenceLiteralDontWiden1.ts, 18, 19)) | ||
>T : Symbol(T, Decl(returnTypeTopLevelInferenceLiteralDontWiden1.ts, 18, 19)) | ||
>T : Symbol(T, Decl(returnTypeTopLevelInferenceLiteralDontWiden1.ts, 18, 19)) | ||
>T : Symbol(T, Decl(returnTypeTopLevelInferenceLiteralDontWiden1.ts, 18, 19)) | ||
|
||
const DTest = D("foo"); | ||
>DTest : Symbol(DTest, Decl(returnTypeTopLevelInferenceLiteralDontWiden1.ts, 20, 5)) | ||
>D : Symbol(D, Decl(returnTypeTopLevelInferenceLiteralDontWiden1.ts, 16, 23)) | ||
|
||
declare function E<T>(value: T): T extends string ? `test_${T}` : T; | ||
>E : Symbol(E, Decl(returnTypeTopLevelInferenceLiteralDontWiden1.ts, 20, 23)) | ||
>T : Symbol(T, Decl(returnTypeTopLevelInferenceLiteralDontWiden1.ts, 22, 19)) | ||
>value : Symbol(value, Decl(returnTypeTopLevelInferenceLiteralDontWiden1.ts, 22, 22)) | ||
>T : Symbol(T, Decl(returnTypeTopLevelInferenceLiteralDontWiden1.ts, 22, 19)) | ||
>T : Symbol(T, Decl(returnTypeTopLevelInferenceLiteralDontWiden1.ts, 22, 19)) | ||
>T : Symbol(T, Decl(returnTypeTopLevelInferenceLiteralDontWiden1.ts, 22, 19)) | ||
>T : Symbol(T, Decl(returnTypeTopLevelInferenceLiteralDontWiden1.ts, 22, 19)) | ||
|
||
const ETest = E("foo"); | ||
>ETest : Symbol(ETest, Decl(returnTypeTopLevelInferenceLiteralDontWiden1.ts, 24, 5)) | ||
>E : Symbol(E, Decl(returnTypeTopLevelInferenceLiteralDontWiden1.ts, 20, 23)) | ||
|
||
declare function F<T>(value: T): T extends number ? `test_${T}` : [T]; | ||
>F : Symbol(F, Decl(returnTypeTopLevelInferenceLiteralDontWiden1.ts, 24, 23)) | ||
>T : Symbol(T, Decl(returnTypeTopLevelInferenceLiteralDontWiden1.ts, 26, 19)) | ||
>value : Symbol(value, Decl(returnTypeTopLevelInferenceLiteralDontWiden1.ts, 26, 22)) | ||
>T : Symbol(T, Decl(returnTypeTopLevelInferenceLiteralDontWiden1.ts, 26, 19)) | ||
>T : Symbol(T, Decl(returnTypeTopLevelInferenceLiteralDontWiden1.ts, 26, 19)) | ||
>T : Symbol(T, Decl(returnTypeTopLevelInferenceLiteralDontWiden1.ts, 26, 19)) | ||
>T : Symbol(T, Decl(returnTypeTopLevelInferenceLiteralDontWiden1.ts, 26, 19)) | ||
|
||
const FTest1 = F("foo"); | ||
>FTest1 : Symbol(FTest1, Decl(returnTypeTopLevelInferenceLiteralDontWiden1.ts, 28, 5)) | ||
>F : Symbol(F, Decl(returnTypeTopLevelInferenceLiteralDontWiden1.ts, 24, 23)) | ||
|
||
const FTest2 = F(42); | ||
>FTest2 : Symbol(FTest2, Decl(returnTypeTopLevelInferenceLiteralDontWiden1.ts, 29, 5)) | ||
>F : Symbol(F, Decl(returnTypeTopLevelInferenceLiteralDontWiden1.ts, 24, 23)) | ||
|
||
declare function G<T>(value: T): T extends string ? `test_${T}` : [T]; | ||
>G : Symbol(G, Decl(returnTypeTopLevelInferenceLiteralDontWiden1.ts, 29, 21)) | ||
>T : Symbol(T, Decl(returnTypeTopLevelInferenceLiteralDontWiden1.ts, 31, 19)) | ||
>value : Symbol(value, Decl(returnTypeTopLevelInferenceLiteralDontWiden1.ts, 31, 22)) | ||
>T : Symbol(T, Decl(returnTypeTopLevelInferenceLiteralDontWiden1.ts, 31, 19)) | ||
>T : Symbol(T, Decl(returnTypeTopLevelInferenceLiteralDontWiden1.ts, 31, 19)) | ||
>T : Symbol(T, Decl(returnTypeTopLevelInferenceLiteralDontWiden1.ts, 31, 19)) | ||
>T : Symbol(T, Decl(returnTypeTopLevelInferenceLiteralDontWiden1.ts, 31, 19)) | ||
|
||
const GTest1 = G("foo"); | ||
>GTest1 : Symbol(GTest1, Decl(returnTypeTopLevelInferenceLiteralDontWiden1.ts, 33, 5)) | ||
>G : Symbol(G, Decl(returnTypeTopLevelInferenceLiteralDontWiden1.ts, 29, 21)) | ||
|
||
const GTest2 = G(42); | ||
>GTest2 : Symbol(GTest2, Decl(returnTypeTopLevelInferenceLiteralDontWiden1.ts, 34, 5)) | ||
>G : Symbol(G, Decl(returnTypeTopLevelInferenceLiteralDontWiden1.ts, 29, 21)) | ||
|
||
declare function H<T>( | ||
>H : Symbol(H, Decl(returnTypeTopLevelInferenceLiteralDontWiden1.ts, 34, 21)) | ||
>T : Symbol(T, Decl(returnTypeTopLevelInferenceLiteralDontWiden1.ts, 36, 19)) | ||
|
||
value: T, | ||
>value : Symbol(value, Decl(returnTypeTopLevelInferenceLiteralDontWiden1.ts, 36, 22)) | ||
>T : Symbol(T, Decl(returnTypeTopLevelInferenceLiteralDontWiden1.ts, 36, 19)) | ||
|
||
): T extends number ? never : `test_${T & string}`; | ||
>T : Symbol(T, Decl(returnTypeTopLevelInferenceLiteralDontWiden1.ts, 36, 19)) | ||
>T : Symbol(T, Decl(returnTypeTopLevelInferenceLiteralDontWiden1.ts, 36, 19)) | ||
|
||
const HTest = H("foo"); | ||
>HTest : Symbol(HTest, Decl(returnTypeTopLevelInferenceLiteralDontWiden1.ts, 40, 5)) | ||
>H : Symbol(H, Decl(returnTypeTopLevelInferenceLiteralDontWiden1.ts, 34, 21)) | ||
|
163 changes: 163 additions & 0 deletions
163
tests/baselines/reference/returnTypeTopLevelInferenceLiteralDontWiden1.types
This file contains 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,163 @@ | ||
//// [tests/cases/compiler/returnTypeTopLevelInferenceLiteralDontWiden1.ts] //// | ||
|
||
=== returnTypeTopLevelInferenceLiteralDontWiden1.ts === | ||
declare function A<T>(value: T): T extends string ? T : never; | ||
>A : <T>(value: T) => T extends string ? T : never | ||
> : ^ ^^ ^^ ^^^^^ | ||
>value : T | ||
> : ^ | ||
|
||
const ATest = A("foo"); | ||
>ATest : "foo" | ||
> : ^^^^^ | ||
>A("foo") : "foo" | ||
> : ^^^^^ | ||
>A : <T>(value: T) => T extends string ? T : never | ||
> : ^ ^^ ^^ ^^^^^ | ||
>"foo" : "foo" | ||
> : ^^^^^ | ||
|
||
declare function B<T>(value: T): `test_${T & string}`; | ||
>B : <T>(value: T) => `test_${T & string}` | ||
> : ^ ^^ ^^ ^^^^^ | ||
>value : T | ||
> : ^ | ||
|
||
const BTest = B("foo"); | ||
>BTest : "test_foo" | ||
> : ^^^^^^^^^^ | ||
>B("foo") : "test_foo" | ||
> : ^^^^^^^^^^ | ||
>B : <T>(value: T) => `test_${T & string}` | ||
> : ^ ^^ ^^ ^^^^^ | ||
>"foo" : "foo" | ||
> : ^^^^^ | ||
|
||
declare function C<T>( | ||
>C : <T>(value: T) => T extends string ? T : T extends number | bigint | boolean | null | undefined ? `test_${T}` : never | ||
> : ^ ^^ ^^ ^^^^^ | ||
|
||
value: T, | ||
>value : T | ||
> : ^ | ||
|
||
): T extends string | ||
? T | ||
: T extends number | bigint | boolean | null | undefined | ||
? `test_${T}` | ||
: never; | ||
|
||
const CTest = C("foo"); | ||
>CTest : "foo" | ||
> : ^^^^^ | ||
>C("foo") : "foo" | ||
> : ^^^^^ | ||
>C : <T>(value: T) => T extends string ? T : T extends number | bigint | boolean | null | undefined ? `test_${T}` : never | ||
> : ^ ^^ ^^ ^^^^^ | ||
>"foo" : "foo" | ||
> : ^^^^^ | ||
|
||
declare function D<T>(value: T): T extends number ? `test_${T}` : T; | ||
>D : <T>(value: T) => T extends number ? `test_${T}` : T | ||
> : ^ ^^ ^^ ^^^^^ | ||
>value : T | ||
> : ^ | ||
|
||
const DTest = D("foo"); | ||
>DTest : "foo" | ||
> : ^^^^^ | ||
>D("foo") : "foo" | ||
> : ^^^^^ | ||
>D : <T>(value: T) => T extends number ? `test_${T}` : T | ||
> : ^ ^^ ^^ ^^^^^ | ||
>"foo" : "foo" | ||
> : ^^^^^ | ||
|
||
declare function E<T>(value: T): T extends string ? `test_${T}` : T; | ||
>E : <T>(value: T) => T extends string ? `test_${T}` : T | ||
> : ^ ^^ ^^ ^^^^^ | ||
>value : T | ||
> : ^ | ||
|
||
const ETest = E("foo"); | ||
>ETest : "test_foo" | ||
> : ^^^^^^^^^^ | ||
>E("foo") : "test_foo" | ||
> : ^^^^^^^^^^ | ||
>E : <T>(value: T) => T extends string ? `test_${T}` : T | ||
> : ^ ^^ ^^ ^^^^^ | ||
>"foo" : "foo" | ||
> : ^^^^^ | ||
|
||
declare function F<T>(value: T): T extends number ? `test_${T}` : [T]; | ||
>F : <T>(value: T) => T extends number ? `test_${T}` : [T] | ||
> : ^ ^^ ^^ ^^^^^ | ||
>value : T | ||
> : ^ | ||
|
||
const FTest1 = F("foo"); | ||
>FTest1 : ["foo"] | ||
> : ^^^^^^^ | ||
>F("foo") : ["foo"] | ||
> : ^^^^^^^ | ||
>F : <T>(value: T) => T extends number ? `test_${T}` : [T] | ||
> : ^ ^^ ^^ ^^^^^ | ||
>"foo" : "foo" | ||
> : ^^^^^ | ||
|
||
const FTest2 = F(42); | ||
>FTest2 : "test_42" | ||
> : ^^^^^^^^^ | ||
>F(42) : "test_42" | ||
> : ^^^^^^^^^ | ||
>F : <T>(value: T) => T extends number ? `test_${T}` : [T] | ||
> : ^ ^^ ^^ ^^^^^ | ||
>42 : 42 | ||
> : ^^ | ||
|
||
declare function G<T>(value: T): T extends string ? `test_${T}` : [T]; | ||
>G : <T>(value: T) => T extends string ? `test_${T}` : [T] | ||
> : ^ ^^ ^^ ^^^^^ | ||
>value : T | ||
> : ^ | ||
|
||
const GTest1 = G("foo"); | ||
>GTest1 : "test_foo" | ||
> : ^^^^^^^^^^ | ||
>G("foo") : "test_foo" | ||
> : ^^^^^^^^^^ | ||
>G : <T>(value: T) => T extends string ? `test_${T}` : [T] | ||
> : ^ ^^ ^^ ^^^^^ | ||
>"foo" : "foo" | ||
> : ^^^^^ | ||
|
||
const GTest2 = G(42); | ||
>GTest2 : [42] | ||
> : ^^^^ | ||
>G(42) : [42] | ||
> : ^^^^ | ||
>G : <T>(value: T) => T extends string ? `test_${T}` : [T] | ||
> : ^ ^^ ^^ ^^^^^ | ||
>42 : 42 | ||
> : ^^ | ||
|
||
declare function H<T>( | ||
>H : <T>(value: T) => T extends number ? never : `test_${T & string}` | ||
> : ^ ^^ ^^ ^^^^^ | ||
|
||
value: T, | ||
>value : T | ||
> : ^ | ||
|
||
): T extends number ? never : `test_${T & string}`; | ||
|
||
const HTest = H("foo"); | ||
>HTest : "test_foo" | ||
> : ^^^^^^^^^^ | ||
>H("foo") : "test_foo" | ||
> : ^^^^^^^^^^ | ||
>H : <T>(value: T) => T extends number ? never : `test_${T & string}` | ||
> : ^ ^^ ^^ ^^^^^ | ||
>"foo" : "foo" | ||
> : ^^^^^ | ||
|
44 changes: 44 additions & 0 deletions
44
tests/cases/compiler/returnTypeTopLevelInferenceLiteralDontWiden1.ts
This file contains 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,44 @@ | ||
// @strict: true | ||
// @noEmit: true | ||
|
||
declare function A<T>(value: T): T extends string ? T : never; | ||
|
||
const ATest = A("foo"); | ||
|
||
declare function B<T>(value: T): `test_${T & string}`; | ||
|
||
const BTest = B("foo"); | ||
|
||
declare function C<T>( | ||
value: T, | ||
): T extends string | ||
? T | ||
: T extends number | bigint | boolean | null | undefined | ||
? `test_${T}` | ||
: never; | ||
|
||
const CTest = C("foo"); | ||
|
||
declare function D<T>(value: T): T extends number ? `test_${T}` : T; | ||
|
||
const DTest = D("foo"); | ||
|
||
declare function E<T>(value: T): T extends string ? `test_${T}` : T; | ||
|
||
const ETest = E("foo"); | ||
|
||
declare function F<T>(value: T): T extends number ? `test_${T}` : [T]; | ||
|
||
const FTest1 = F("foo"); | ||
const FTest2 = F(42); | ||
|
||
declare function G<T>(value: T): T extends string ? `test_${T}` : [T]; | ||
|
||
const GTest1 = G("foo"); | ||
const GTest2 = G(42); | ||
|
||
declare function H<T>( | ||
value: T, | ||
): T extends number ? never : `test_${T & string}`; | ||
|
||
const HTest = H("foo"); |
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.
TODO: handle
TypeFlags.StringMapping
too if this part of the PR turns out to be seen as a positive change too