diff --git a/src/services/codefixes/fixAddMissingMember.ts b/src/services/codefixes/fixAddMissingMember.ts index 490d65171be..80fdb0380ad 100644 --- a/src/services/codefixes/fixAddMissingMember.ts +++ b/src/services/codefixes/fixAddMissingMember.ts @@ -330,7 +330,7 @@ function getInfo(sourceFile: SourceFile, tokenPos: number, errorCode: number, ch } if (isIdentifier(token)) { - const type = checker.getContextualType(token); + const type = checker.getContextualType(token)?.getNonNullableType(); if (type && getObjectFlags(type) & ObjectFlags.Anonymous) { const signature = firstOrUndefined(checker.getSignaturesOfType(type, SignatureKind.Call)); if (signature === undefined) return undefined; diff --git a/tests/cases/fourslash/codeFixAddMissingFunctionDeclaration26.ts b/tests/cases/fourslash/codeFixAddMissingFunctionDeclaration26.ts new file mode 100644 index 00000000000..156be5141e8 --- /dev/null +++ b/tests/cases/fourslash/codeFixAddMissingFunctionDeclaration26.ts @@ -0,0 +1,29 @@ +/// + +// @strict: true + +////interface Foo { +//// a: ((e: any) => void) | null; +////} +//// +////const foo: Foo = { +//// a: fn +////} + +verify.codeFix({ + index: 0, + description: [ts.Diagnostics.Add_missing_function_declaration_0.message, "fn"], + newFileContent: +`interface Foo { + a: ((e: any) => void) | null; +} + +const foo: Foo = { + a: fn +} + +function fn(e: any): void { + throw new Error("Function not implemented."); +} +` +}); diff --git a/tests/cases/fourslash/codeFixAddMissingFunctionDeclaration27.ts b/tests/cases/fourslash/codeFixAddMissingFunctionDeclaration27.ts new file mode 100644 index 00000000000..363dac2a30d --- /dev/null +++ b/tests/cases/fourslash/codeFixAddMissingFunctionDeclaration27.ts @@ -0,0 +1,29 @@ +/// + +// @strict: true + +////interface Foo { +//// a?: ((e: any) => void); +////} +//// +////const foo: Foo = { +//// a: fn +////} + +verify.codeFix({ + index: 0, + description: [ts.Diagnostics.Add_missing_function_declaration_0.message, "fn"], + newFileContent: +`interface Foo { + a?: ((e: any) => void); +} + +const foo: Foo = { + a: fn +} + +function fn(e: any): void { + throw new Error("Function not implemented."); +} +` +});