Fix quick fix for isolatedDeclarations to keep trailing unknown in generics (#61227)

This commit is contained in:
Ben Lickly 2025-02-19 17:28:50 -08:00 committed by GitHub
parent 71b16ea186
commit 3f416e0f52
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 75 additions and 0 deletions

View File

@ -621,6 +621,9 @@ function endOfRequiredTypeParameters(checker: TypeChecker, type: GenericType): n
const fullTypeArguments = type.typeArguments;
const target = type.target;
for (let cutoff = 0; cutoff < fullTypeArguments.length; cutoff++) {
if (target.localTypeParameters?.[cutoff].constraint === undefined) {
continue;
}
const typeArguments = fullTypeArguments.slice(0, cutoff);
const filledIn = checker.fillMissingTypeArguments(typeArguments, target.typeParameters, cutoff, /*isJavaScriptImplicitAny*/ false);
if (filledIn.every((fill, i) => fill === fullTypeArguments[i])) {

View File

@ -0,0 +1,19 @@
/// <reference path='fourslash.ts'/>
// @isolatedDeclarations: true
// @declaration: true
// @lib: es2015
////
////let x: unknown;
////export const s = new Set([x]);
////
verify.codeFix({
description: "Add annotation of type 'Set<unknown>'",
index: 0,
newFileContent:
`
let x: unknown;
export const s: Set<unknown> = new Set([x]);
`,
});

View File

@ -0,0 +1,17 @@
/// <reference path='fourslash.ts'/>
// @isolatedDeclarations: true
// @declaration: true
// @lib: es2015
////
////export const s = new Set<unknown>();
////
verify.codeFix({
description: "Add annotation of type 'Set<unknown>'",
index: 0,
newFileContent:
`
export const s: Set<unknown> = new Set<unknown>();
`,
});

View File

@ -0,0 +1,18 @@
/// <reference path='fourslash.ts'/>
// @isolatedDeclarations: true
// @declaration: true
////
////export interface Foo<S = string, T = unknown, U = number> {}
////export function g(x: Foo<number, unknown, number>) { return x; }
////
verify.codeFix({
description: "Add return type 'Foo<number>'",
index: 0,
newFileContent:
`
export interface Foo<S = string, T = unknown, U = number> {}
export function g(x: Foo<number, unknown, number>): Foo<number> { return x; }
`,
});

View File

@ -0,0 +1,18 @@
/// <reference path='fourslash.ts'/>
// @isolatedDeclarations: true
// @declaration: true
////
////export interface Foo<S = string, T = unknown> {}
////export function f(x: Foo<string, unknown>) { return x; }
////
verify.codeFix({
description: "Add return type 'Foo'",
index: 0,
newFileContent:
`
export interface Foo<S = string, T = unknown> {}
export function f(x: Foo<string, unknown>): Foo { return x; }
`,
});