From b3f9ec3796285d1d0e0eb1490b164900c576fcf4 Mon Sep 17 00:00:00 2001 From: Andy Date: Tue, 19 Jun 2018 16:36:18 -0700 Subject: [PATCH] Fix bug: Still implement a method even if the return type is defined in another file (#24978) --- src/compiler/checker.ts | 9 +++---- src/compiler/types.ts | 1 + ...ClassImplementInterface_typeInOtherFile.ts | 25 +++++++++++++++++++ ...nferFromUsageSetterWithInaccessibleType.ts | 11 +++++++- 4 files changed, 40 insertions(+), 6 deletions(-) create mode 100644 tests/cases/fourslash/codeFixClassImplementInterface_typeInOtherFile.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 1ecf30690a0..ff1da5db911 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -4110,15 +4110,14 @@ namespace ts { if (declaration.parent && declaration.parent.kind === SyntaxKind.VariableDeclaration) { return declarationNameToString((declaration.parent).name); } - if (context && !context.encounteredError && !(context.flags & NodeBuilderFlags.AllowAnonymousIdentifier)) { - context.encounteredError = true; - } switch (declaration.kind) { case SyntaxKind.ClassExpression: - return "(Anonymous class)"; case SyntaxKind.FunctionExpression: case SyntaxKind.ArrowFunction: - return "(Anonymous function)"; + if (context && !context.encounteredError && !(context.flags & NodeBuilderFlags.AllowAnonymousIdentifier)) { + context.encounteredError = true; + } + return declaration.kind === SyntaxKind.ClassExpression ? "(Anonymous class)" : "(Anonymous function)"; } } const nameType = symbol.nameType; diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 9e520241fae..b1e52d68bc0 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -3066,6 +3066,7 @@ namespace ts { Subtype } + // NOTE: If modifying this enum, must modify `TypeFormatFlags` too! export const enum NodeBuilderFlags { None = 0, // Options diff --git a/tests/cases/fourslash/codeFixClassImplementInterface_typeInOtherFile.ts b/tests/cases/fourslash/codeFixClassImplementInterface_typeInOtherFile.ts new file mode 100644 index 00000000000..712bf48e059 --- /dev/null +++ b/tests/cases/fourslash/codeFixClassImplementInterface_typeInOtherFile.ts @@ -0,0 +1,25 @@ +/// + +// @Filename: /I.ts +////export interface J {} +////export interface I { +//// x: J; +//// m(): J; +////} + +// @Filename: /C.ts +////import { I } from "./I"; +////export class C implements I {} + +goTo.file("/C.ts"); +verify.codeFix({ + description: "Implement interface 'I'", + newFileContent: +`import { I } from "./I"; +export class C implements I { + x: import("/I").J; + m(): import("/I").J { + throw new Error("Method not implemented."); + } +}`, +}); diff --git a/tests/cases/fourslash/codeFixInferFromUsageSetterWithInaccessibleType.ts b/tests/cases/fourslash/codeFixInferFromUsageSetterWithInaccessibleType.ts index 9e56fc70734..b56c6e05f69 100644 --- a/tests/cases/fourslash/codeFixInferFromUsageSetterWithInaccessibleType.ts +++ b/tests/cases/fourslash/codeFixInferFromUsageSetterWithInaccessibleType.ts @@ -13,4 +13,13 @@ ////} goTo.file("/b.ts"); -verify.not.codeFixAvailable(); +verify.codeFix({ + index: 0, + description: "Infer type of 'x' from usage", + newFileContent: +`export class C { + set x(val: Promise) { val; } + method() { this.x = import("./a"); } +}`, +}); +