Skip to content

Fixed "add missing properties" codefix for positions with nullable contextual types #60328

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/services/codefixes/fixAddMissingMember.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

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

Expand Down
1 change: 0 additions & 1 deletion tests/cases/fourslash/codeFixAddMissingProperties30.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
////}
////[|f([{}])|]

debugger;
verify.codeFix({
index: 0,
description: ts.Diagnostics.Add_missing_properties.message,
Expand Down
1 change: 0 additions & 1 deletion tests/cases/fourslash/codeFixAddMissingProperties31.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
////}
////[|const b: B[] = [{c: [{}]}]|]

debugger;
verify.codeFix({
index: 0,
description: ts.Diagnostics.Add_missing_properties.message,
Expand Down
15 changes: 15 additions & 0 deletions tests/cases/fourslash/codeFixAddMissingProperties45.ts
Original file line number Diff line number Diff line change
@@ -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: ""
}`,
});
16 changes: 16 additions & 0 deletions tests/cases/fourslash/codeFixAddMissingProperties46.ts
Original file line number Diff line number Diff line change
@@ -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: ""
}`,
});
22 changes: 22 additions & 0 deletions tests/cases/fourslash/codeFixAddMissingProperties47.ts
Original file line number Diff line number Diff line change
@@ -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: ""
}]);`,
});
22 changes: 22 additions & 0 deletions tests/cases/fourslash/codeFixAddMissingProperties48.ts
Original file line number Diff line number Diff line change
@@ -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: ""
}] }];`,
});