Skip to content
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
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25741,12 +25741,15 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}

function isTypeParameterAtTopLevel(type: Type, tp: TypeParameter, depth = 0): boolean {
if (type.flags & TypeFlags.Substitution) {
type = getActualTypeVariable(type);
}
return !!(type === tp ||
type.flags & TypeFlags.UnionOrIntersection && some((type as UnionOrIntersectionType).types, t => isTypeParameterAtTopLevel(t, tp, depth)) ||
depth < 3 && type.flags & TypeFlags.Conditional && (
isTypeParameterAtTopLevel(getTrueTypeFromConditionalType(type as ConditionalType), tp, depth + 1) ||
isTypeParameterAtTopLevel(getFalseTypeFromConditionalType(type as ConditionalType), tp, depth + 1)
));
depth < 3 && (type.flags & TypeFlags.Conditional && (
isTypeParameterAtTopLevel(getTrueTypeFromConditionalType(type as ConditionalType), tp, depth + 1) ||
isTypeParameterAtTopLevel(getFalseTypeFromConditionalType(type as ConditionalType), tp, depth + 1)
) || type.flags & TypeFlags.TemplateLiteral && some((type as TemplateLiteralType).types, t => isTypeParameterAtTopLevel(t, tp, depth))));
Copy link
Contributor Author

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

}

function isTypeParameterAtTopLevelInReturnType(signature: Signature, typeParameter: TypeParameter) {
Expand Down
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))

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"
> : ^^^^^

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");