Fixed "add missing properties" codefix for positions with nullable contextual types (#60328)
This commit is contained in:
Родитель
437d7f7d9c
Коммит
6a90111d05
|
@ -315,13 +315,13 @@ function getInfo(sourceFile: SourceFile, tokenPos: number, errorCode: number, ch
|
|||
const param = signature.parameters[argIndex].valueDeclaration;
|
||||
if (!(param && isParameter(param) && isIdentifier(param.name))) return undefined;
|
||||
|
||||
const properties = arrayFrom(checker.getUnmatchedProperties(checker.getTypeAtLocation(parent), checker.getParameterType(signature, argIndex), /*requireOptionalProperties*/ false, /*matchDiscriminantProperties*/ false));
|
||||
const properties = arrayFrom(checker.getUnmatchedProperties(checker.getTypeAtLocation(parent), checker.getParameterType(signature, argIndex).getNonNullableType(), /*requireOptionalProperties*/ false, /*matchDiscriminantProperties*/ false));
|
||||
if (!length(properties)) return undefined;
|
||||
return { kind: InfoKind.ObjectLiteral, token: param.name, identifier: param.name.text, properties, parentDeclaration: parent };
|
||||
}
|
||||
|
||||
if (token.kind === SyntaxKind.OpenBraceToken && isObjectLiteralExpression(parent)) {
|
||||
const targetType = checker.getContextualType(parent) || checker.getTypeAtLocation(parent);
|
||||
const targetType = (checker.getContextualType(parent) || checker.getTypeAtLocation(parent))?.getNonNullableType();
|
||||
const properties = arrayFrom(checker.getUnmatchedProperties(checker.getTypeAtLocation(parent), targetType, /*requireOptionalProperties*/ false, /*matchDiscriminantProperties*/ false));
|
||||
if (!length(properties)) return undefined;
|
||||
|
||||
|
@ -334,7 +334,7 @@ function getInfo(sourceFile: SourceFile, tokenPos: number, errorCode: number, ch
|
|||
if (!isMemberName(token)) return undefined;
|
||||
|
||||
if (isIdentifier(token) && hasInitializer(parent) && parent.initializer && isObjectLiteralExpression(parent.initializer)) {
|
||||
const targetType = checker.getContextualType(token) || checker.getTypeAtLocation(token);
|
||||
const targetType = (checker.getContextualType(token) || checker.getTypeAtLocation(token))?.getNonNullableType();
|
||||
const properties = arrayFrom(checker.getUnmatchedProperties(checker.getTypeAtLocation(parent.initializer), targetType, /*requireOptionalProperties*/ false, /*matchDiscriminantProperties*/ false));
|
||||
if (!length(properties)) return undefined;
|
||||
|
||||
|
|
|
@ -9,7 +9,6 @@
|
|||
////}
|
||||
////[|f([{}])|]
|
||||
|
||||
debugger;
|
||||
verify.codeFix({
|
||||
index: 0,
|
||||
description: ts.Diagnostics.Add_missing_properties.message,
|
||||
|
|
|
@ -9,7 +9,6 @@
|
|||
////}
|
||||
////[|const b: B[] = [{c: [{}]}]|]
|
||||
|
||||
debugger;
|
||||
verify.codeFix({
|
||||
index: 0,
|
||||
description: ts.Diagnostics.Add_missing_properties.message,
|
||||
|
|
|
@ -0,0 +1,15 @@
|
|||
/// <reference path='fourslash.ts' />
|
||||
|
||||
// @strict: true
|
||||
|
||||
//// type U = { u?: { v: string } };
|
||||
//// const u: U = { [|u: {}|] };
|
||||
|
||||
verify.codeFix({
|
||||
index: 0,
|
||||
description: ts.Diagnostics.Add_missing_properties.message,
|
||||
newRangeContent:
|
||||
`u: {
|
||||
v: ""
|
||||
}`,
|
||||
});
|
|
@ -0,0 +1,16 @@
|
|||
/// <reference path='fourslash.ts' />
|
||||
|
||||
// @strict: true
|
||||
|
||||
//// type T = { t: string };
|
||||
//// declare function f(arg?: T): void;
|
||||
//// f([|{}|]);
|
||||
|
||||
verify.codeFix({
|
||||
index: 0,
|
||||
description: ts.Diagnostics.Add_missing_properties.message,
|
||||
newRangeContent:
|
||||
`{
|
||||
t: ""
|
||||
}`,
|
||||
});
|
|
@ -0,0 +1,22 @@
|
|||
/// <reference path='fourslash.ts' />
|
||||
|
||||
// @strict: true
|
||||
|
||||
//// interface A {
|
||||
//// a: number;
|
||||
//// b: string;
|
||||
//// }
|
||||
//// function f(_obj: (A | undefined)[]): string {
|
||||
//// return "";
|
||||
//// }
|
||||
//// [|f([{}]);|]
|
||||
|
||||
verify.codeFix({
|
||||
index: 0,
|
||||
description: ts.Diagnostics.Add_missing_properties.message,
|
||||
newRangeContent:
|
||||
`f([{
|
||||
a: 0,
|
||||
b: ""
|
||||
}]);`,
|
||||
});
|
|
@ -0,0 +1,22 @@
|
|||
/// <reference path='fourslash.ts' />
|
||||
|
||||
// @strict: true
|
||||
|
||||
//// interface A {
|
||||
//// a: number;
|
||||
//// b: string;
|
||||
//// }
|
||||
//// interface B {
|
||||
//// c: (A | undefined)[];
|
||||
//// }
|
||||
//// [|const b: B[] = [{ c: [{}] }];|]
|
||||
|
||||
verify.codeFix({
|
||||
index: 0,
|
||||
description: ts.Diagnostics.Add_missing_properties.message,
|
||||
newRangeContent:
|
||||
`const b: B[] = [{ c: [{
|
||||
a: 0,
|
||||
b: ""
|
||||
}] }];`,
|
||||
});
|
Загрузка…
Ссылка в новой задаче