diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 18c3b6e1ec685..35d54c3468774 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -1763,6 +1763,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { getImmediateAliasedSymbol, getAliasedSymbol: resolveAlias, getEmitResolver, + requiresAddingImplicitUndefined, getExportsOfModule: getExportsOfModuleAsArray, getExportsAndPropertiesOfModule, forEachExportAndPropertyOfModule, diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 4994322657070..9df1b4a8e7f31 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -5265,6 +5265,7 @@ export interface TypeChecker { /** @internal */ getDiagnostics(sourceFile?: SourceFile, cancellationToken?: CancellationToken): Diagnostic[]; /** @internal */ getGlobalDiagnostics(): Diagnostic[]; /** @internal */ getEmitResolver(sourceFile?: SourceFile, cancellationToken?: CancellationToken, forceDts?: boolean): EmitResolver; + /** @internal */ requiresAddingImplicitUndefined(parameter: ParameterDeclaration | JSDocParameterTag): boolean; /** @internal */ getNodeCount(): number; /** @internal */ getIdentifierCount(): number; diff --git a/src/services/codefixes/fixMissingTypeAnnotationOnExports.ts b/src/services/codefixes/fixMissingTypeAnnotationOnExports.ts index 1b2f93d06c78d..70ceb0c31eba3 100644 --- a/src/services/codefixes/fixMissingTypeAnnotationOnExports.ts +++ b/src/services/codefixes/fixMissingTypeAnnotationOnExports.ts @@ -183,7 +183,7 @@ registerCodeFix({ addCodeAction(addInlineTypeAssertion, fixes, context, TypePrintMode.Full, f => f.addInlineAssertion(context.span)); addCodeAction(addInlineTypeAssertion, fixes, context, TypePrintMode.Relative, f => f.addInlineAssertion(context.span)); - addCodeAction(addAnnotationFix, fixes, context, TypePrintMode.Widened, f => f.addInlineAssertion(context.span)); + addCodeAction(addInlineTypeAssertion, fixes, context, TypePrintMode.Widened, f => f.addInlineAssertion(context.span)); addCodeAction(extractExpression, fixes, context, TypePrintMode.Full, f => f.extractAsVariable(context.span)); @@ -237,7 +237,6 @@ function withContext( const sourceFile: SourceFile = context.sourceFile; const program = context.program; const typeChecker: TypeChecker = program.getTypeChecker(); - const emitResolver = typeChecker.getEmitResolver(); const scriptTarget = getEmitScriptTarget(program.getCompilerOptions()); const importAdder = createImportAdder(context.sourceFile, context.program, context.preferences, context.host); const fixedNodes = new Set(); @@ -887,7 +886,7 @@ function withContext( type = widenedType; } - if (isParameter(node) && emitResolver.requiresAddingImplicitUndefined(node)) { + if (isParameter(node) && typeChecker.requiresAddingImplicitUndefined(node)) { type = typeChecker.getUnionType([typeChecker.getUndefinedType(), type], UnionReduction.None); } const flags = ( @@ -1108,26 +1107,26 @@ function withContext( setEmitFlags(node, EmitFlags.None); return result; } -} -// Some --isolatedDeclarations errors are not present on the node that directly needs type annotation, so look in the -// ancestors to look for node that needs type annotation. This function can return undefined if the AST is ill-formed. -function findAncestorWithMissingType(node: Node): Node | undefined { - return findAncestor(node, n => { - return canHaveTypeAnnotation.has(n.kind) && - ((!isObjectBindingPattern(n) && !isArrayBindingPattern(n)) || isVariableDeclaration(n.parent)); - }); -} - -function findBestFittingNode(node: Node, span: TextSpan) { - while (node && node.end < span.start + span.length) { - node = node.parent; - } - while (node.parent.pos === node.pos && node.parent.end === node.end) { - node = node.parent; + // Some --isolatedDeclarations errors are not present on the node that directly needs type annotation, so look in the + // ancestors to look for node that needs type annotation. This function can return undefined if the AST is ill-formed. + function findAncestorWithMissingType(node: Node): Node | undefined { + return findAncestor(node, n => { + return canHaveTypeAnnotation.has(n.kind) && + ((!isObjectBindingPattern(n) && !isArrayBindingPattern(n)) || isVariableDeclaration(n.parent)); + }); } - if (isIdentifier(node) && hasInitializer(node.parent) && node.parent.initializer) { - return node.parent.initializer; + + function findBestFittingNode(node: Node, span: TextSpan) { + while (node && node.end < span.start + span.length) { + node = node.parent; + } + while (node.parent.pos === node.pos && node.parent.end === node.end) { + node = node.parent; + } + if (isIdentifier(node) && hasInitializer(node.parent) && node.parent.initializer) { + return node.parent.initializer; + } + return node; } - return node; }