quick fix for a nullable missing callback function (#51743)

This commit is contained in:
Lyu, Wei-Da 2022-12-06 04:23:59 +08:00 committed by GitHub
parent 9e845d2248
commit 0c09d2f172
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 59 additions and 1 deletions

View File

@ -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;

View File

@ -0,0 +1,29 @@
/// <reference path="fourslash.ts" />
// @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.");
}
`
});

View File

@ -0,0 +1,29 @@
/// <reference path="fourslash.ts" />
// @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.");
}
`
});