Skip to content

Commit

Permalink
Fix quick fix for isolatedDeclarations to keep trailing unknown in ge…
Browse files Browse the repository at this point in the history
…nerics (#61227)
  • Loading branch information
blickly authored Feb 20, 2025
1 parent 71b16ea commit 3f416e0
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/services/codefixes/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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])) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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]);
`,
});
Original file line number Diff line number Diff line change
@@ -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>();
`,
});
Original file line number Diff line number Diff line change
@@ -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; }
`,
});
Original file line number Diff line number Diff line change
@@ -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; }
`,
});

0 comments on commit 3f416e0

Please sign in to comment.