Skip to content

Commit 6a90111

Browse files
authored
Fixed "add missing properties" codefix for positions with nullable contextual types (#60328)
1 parent 437d7f7 commit 6a90111

7 files changed

+78
-5
lines changed

src/services/codefixes/fixAddMissingMember.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -315,13 +315,13 @@ function getInfo(sourceFile: SourceFile, tokenPos: number, errorCode: number, ch
315315
const param = signature.parameters[argIndex].valueDeclaration;
316316
if (!(param && isParameter(param) && isIdentifier(param.name))) return undefined;
317317

318-
const properties = arrayFrom(checker.getUnmatchedProperties(checker.getTypeAtLocation(parent), checker.getParameterType(signature, argIndex), /*requireOptionalProperties*/ false, /*matchDiscriminantProperties*/ false));
318+
const properties = arrayFrom(checker.getUnmatchedProperties(checker.getTypeAtLocation(parent), checker.getParameterType(signature, argIndex).getNonNullableType(), /*requireOptionalProperties*/ false, /*matchDiscriminantProperties*/ false));
319319
if (!length(properties)) return undefined;
320320
return { kind: InfoKind.ObjectLiteral, token: param.name, identifier: param.name.text, properties, parentDeclaration: parent };
321321
}
322322

323323
if (token.kind === SyntaxKind.OpenBraceToken && isObjectLiteralExpression(parent)) {
324-
const targetType = checker.getContextualType(parent) || checker.getTypeAtLocation(parent);
324+
const targetType = (checker.getContextualType(parent) || checker.getTypeAtLocation(parent))?.getNonNullableType();
325325
const properties = arrayFrom(checker.getUnmatchedProperties(checker.getTypeAtLocation(parent), targetType, /*requireOptionalProperties*/ false, /*matchDiscriminantProperties*/ false));
326326
if (!length(properties)) return undefined;
327327

@@ -334,7 +334,7 @@ function getInfo(sourceFile: SourceFile, tokenPos: number, errorCode: number, ch
334334
if (!isMemberName(token)) return undefined;
335335

336336
if (isIdentifier(token) && hasInitializer(parent) && parent.initializer && isObjectLiteralExpression(parent.initializer)) {
337-
const targetType = checker.getContextualType(token) || checker.getTypeAtLocation(token);
337+
const targetType = (checker.getContextualType(token) || checker.getTypeAtLocation(token))?.getNonNullableType();
338338
const properties = arrayFrom(checker.getUnmatchedProperties(checker.getTypeAtLocation(parent.initializer), targetType, /*requireOptionalProperties*/ false, /*matchDiscriminantProperties*/ false));
339339
if (!length(properties)) return undefined;
340340

tests/cases/fourslash/codeFixAddMissingProperties30.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
////}
1010
////[|f([{}])|]
1111

12-
debugger;
1312
verify.codeFix({
1413
index: 0,
1514
description: ts.Diagnostics.Add_missing_properties.message,

tests/cases/fourslash/codeFixAddMissingProperties31.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
////}
1010
////[|const b: B[] = [{c: [{}]}]|]
1111

12-
debugger;
1312
verify.codeFix({
1413
index: 0,
1514
description: ts.Diagnostics.Add_missing_properties.message,
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @strict: true
4+
5+
//// type U = { u?: { v: string } };
6+
//// const u: U = { [|u: {}|] };
7+
8+
verify.codeFix({
9+
index: 0,
10+
description: ts.Diagnostics.Add_missing_properties.message,
11+
newRangeContent:
12+
`u: {
13+
v: ""
14+
}`,
15+
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @strict: true
4+
5+
//// type T = { t: string };
6+
//// declare function f(arg?: T): void;
7+
//// f([|{}|]);
8+
9+
verify.codeFix({
10+
index: 0,
11+
description: ts.Diagnostics.Add_missing_properties.message,
12+
newRangeContent:
13+
`{
14+
t: ""
15+
}`,
16+
});
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @strict: true
4+
5+
//// interface A {
6+
//// a: number;
7+
//// b: string;
8+
//// }
9+
//// function f(_obj: (A | undefined)[]): string {
10+
//// return "";
11+
//// }
12+
//// [|f([{}]);|]
13+
14+
verify.codeFix({
15+
index: 0,
16+
description: ts.Diagnostics.Add_missing_properties.message,
17+
newRangeContent:
18+
`f([{
19+
a: 0,
20+
b: ""
21+
}]);`,
22+
});
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @strict: true
4+
5+
//// interface A {
6+
//// a: number;
7+
//// b: string;
8+
//// }
9+
//// interface B {
10+
//// c: (A | undefined)[];
11+
//// }
12+
//// [|const b: B[] = [{ c: [{}] }];|]
13+
14+
verify.codeFix({
15+
index: 0,
16+
description: ts.Diagnostics.Add_missing_properties.message,
17+
newRangeContent:
18+
`const b: B[] = [{ c: [{
19+
a: 0,
20+
b: ""
21+
}] }];`,
22+
});

0 commit comments

Comments
 (0)