Skip to content

[WIP] Invalid quick fix for function returning Promise<unknown> with isolatedDeclarations #61856

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

Closed
wants to merge 2 commits into from
Closed
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
38 changes: 22 additions & 16 deletions src/services/codefixes/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -616,22 +616,28 @@ export function typeNodeToAutoImportableTypeNode(typeNode: TypeNode, importAdder
return getSynthesizedDeepClone(typeNode);
}

function endOfRequiredTypeParameters(checker: TypeChecker, type: GenericType): number {
Debug.assert(type.typeArguments);
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])) {
return cutoff;
}
}
// If we make it all the way here, all the type arguments are required.
return fullTypeArguments.length;
function endOfRequiredTypeParameters(checker: TypeChecker, type: GenericType): number {
Debug.assert(type.typeArguments);
const fullTypeArguments = type.typeArguments;
const target = type.target;

for (let cutoff = 0; cutoff < fullTypeArguments.length; cutoff++) {
// Special handling for Promise<unknown> - don't minimize it to just Promise
// This fixes the issue where Promise<unknown> was being incorrectly minimized
if (target && target.symbol && target.symbol.name === "Promise" &&
fullTypeArguments.length === 1 &&
checker.typeToString(fullTypeArguments[0]) === "unknown") {
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])) {
return cutoff;
}
}
// If we make it all the way here, all the type arguments are required.
return fullTypeArguments.length;
}

/** @internal */
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/// <reference path='fourslash.ts'/>

// @isolatedDeclarations: true
// @declaration: true
//// export function waitUnknown(ms: number) {
//// return new Promise(() => {});
//// }

verify.codeFix({
description: ts.Diagnostics.Add_return_type_0.message,
index: 0,
newFileContent:
`export function waitUnknown(ms: number): Promise<unknown> {
return new Promise(() => {});
}`
});