Skip to content

Commit 3f416e0

Browse files
authored
Fix quick fix for isolatedDeclarations to keep trailing unknown in generics (#61227)
1 parent 71b16ea commit 3f416e0

5 files changed

+75
-0
lines changed

src/services/codefixes/helpers.ts

+3
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,9 @@ function endOfRequiredTypeParameters(checker: TypeChecker, type: GenericType): n
621621
const fullTypeArguments = type.typeArguments;
622622
const target = type.target;
623623
for (let cutoff = 0; cutoff < fullTypeArguments.length; cutoff++) {
624+
if (target.localTypeParameters?.[cutoff].constraint === undefined) {
625+
continue;
626+
}
624627
const typeArguments = fullTypeArguments.slice(0, cutoff);
625628
const filledIn = checker.fillMissingTypeArguments(typeArguments, target.typeParameters, cutoff, /*isJavaScriptImplicitAny*/ false);
626629
if (filledIn.every((fill, i) => fill === fullTypeArguments[i])) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/// <reference path='fourslash.ts'/>
2+
3+
// @isolatedDeclarations: true
4+
// @declaration: true
5+
// @lib: es2015
6+
////
7+
////let x: unknown;
8+
////export const s = new Set([x]);
9+
////
10+
11+
verify.codeFix({
12+
description: "Add annotation of type 'Set<unknown>'",
13+
index: 0,
14+
newFileContent:
15+
`
16+
let x: unknown;
17+
export const s: Set<unknown> = new Set([x]);
18+
`,
19+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/// <reference path='fourslash.ts'/>
2+
3+
// @isolatedDeclarations: true
4+
// @declaration: true
5+
// @lib: es2015
6+
////
7+
////export const s = new Set<unknown>();
8+
////
9+
10+
verify.codeFix({
11+
description: "Add annotation of type 'Set<unknown>'",
12+
index: 0,
13+
newFileContent:
14+
`
15+
export const s: Set<unknown> = new Set<unknown>();
16+
`,
17+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/// <reference path='fourslash.ts'/>
2+
3+
// @isolatedDeclarations: true
4+
// @declaration: true
5+
////
6+
////export interface Foo<S = string, T = unknown, U = number> {}
7+
////export function g(x: Foo<number, unknown, number>) { return x; }
8+
////
9+
10+
verify.codeFix({
11+
description: "Add return type 'Foo<number>'",
12+
index: 0,
13+
newFileContent:
14+
`
15+
export interface Foo<S = string, T = unknown, U = number> {}
16+
export function g(x: Foo<number, unknown, number>): Foo<number> { return x; }
17+
`,
18+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/// <reference path='fourslash.ts'/>
2+
3+
// @isolatedDeclarations: true
4+
// @declaration: true
5+
////
6+
////export interface Foo<S = string, T = unknown> {}
7+
////export function f(x: Foo<string, unknown>) { return x; }
8+
////
9+
10+
verify.codeFix({
11+
description: "Add return type 'Foo'",
12+
index: 0,
13+
newFileContent:
14+
`
15+
export interface Foo<S = string, T = unknown> {}
16+
export function f(x: Foo<string, unknown>): Foo { return x; }
17+
`,
18+
});

0 commit comments

Comments
 (0)