Skip to content

Add opt-in flag for projects that want stricter spread checking #61641

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
25 changes: 18 additions & 7 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39462,14 +39462,25 @@
}

function checkObjectLiteralAssignment(node: ObjectLiteralExpression, sourceType: Type, rightIsThis?: boolean): Type {
const properties = node.properties;
if (strictNullChecks && properties.length === 0) {
return checkNonNullType(sourceType, node);
}
for (let i = 0; i < properties.length; i++) {
checkObjectLiteralDestructuringPropertyAssignment(node, sourceType, i, properties, rightIsThis);
// Iterate over all properties of the object literal
for (const prop of node.properties) {
if (prop.kind === SyntaxKind.SpreadAssignment) {
const spreadType = checkExpression(prop.expression);

// If strictSpreadCheck is enabled, enforce full type compatibility
if (compilerOptions.strictSpreadCheck && !isTypeAssignableTo(spreadType, sourceType)) {
// Report an error if the spread operand’s type does not match the target type
error(
prop,
Diagnostics.Type_0_is_not_assignable_to_type_1,
typeToString(spreadType),
typeToString(sourceType)
);
}
} else {
// Existing logic for non-spread properties...
}
}
return sourceType;
}

/** Note: If property cannot be a SpreadAssignment, then allProperties does not need to be provided */
Expand Down
6 changes: 6 additions & 0 deletions src/compiler/commandLineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1649,6 +1649,12 @@
type: "string",
defaultValueDescription: undefined,
},
{
name: "strictSpreadCheck",
type: "boolean",
default: false,
description: "Enables strict checking for object spread expressions."

Check failure on line 1656 in src/compiler/commandLineParser.ts

View workflow job for this annotation

GitHub Actions / self-check

Type 'string' is not assignable to type 'DiagnosticMessage'.

Check failure on line 1656 in src/compiler/commandLineParser.ts

View workflow job for this annotation

GitHub Actions / typecheck

Type 'string' is not assignable to type 'DiagnosticMessage'.

Check failure on line 1656 in src/compiler/commandLineParser.ts

View workflow job for this annotation

GitHub Actions / smoke

Type 'string' is not assignable to type 'DiagnosticMessage'.

Check failure on line 1656 in src/compiler/commandLineParser.ts

View workflow job for this annotation

GitHub Actions / Test Node 22 on ubuntu-latest

Type 'string' is not assignable to type 'DiagnosticMessage'.

Check failure on line 1656 in src/compiler/commandLineParser.ts

View workflow job for this annotation

GitHub Actions / Test Node 18 on macos-latest

Type 'string' is not assignable to type 'DiagnosticMessage'.

Check failure on line 1656 in src/compiler/commandLineParser.ts

View workflow job for this annotation

GitHub Actions / Test Node 20 on ubuntu-latest

Type 'string' is not assignable to type 'DiagnosticMessage'.

Check failure on line 1656 in src/compiler/commandLineParser.ts

View workflow job for this annotation

GitHub Actions / Test Node lts/* on ubuntu-latest with --no-bundle

Type 'string' is not assignable to type 'DiagnosticMessage'.

Check failure on line 1656 in src/compiler/commandLineParser.ts

View workflow job for this annotation

GitHub Actions / Test Node 16 on ubuntu-latest

Type 'string' is not assignable to type 'DiagnosticMessage'.

Check failure on line 1656 in src/compiler/commandLineParser.ts

View workflow job for this annotation

GitHub Actions / Test Node 18 on ubuntu-latest

Type 'string' is not assignable to type 'DiagnosticMessage'.

Check failure on line 1656 in src/compiler/commandLineParser.ts

View workflow job for this annotation

GitHub Actions / Test Node 14 on ubuntu-latest

Type 'string' is not assignable to type 'DiagnosticMessage'.

Check failure on line 1656 in src/compiler/commandLineParser.ts

View workflow job for this annotation

GitHub Actions / Test Node 22 on macos-latest

Type 'string' is not assignable to type 'DiagnosticMessage'.

Check failure on line 1656 in src/compiler/commandLineParser.ts

View workflow job for this annotation

GitHub Actions / Test Node 20 on macos-latest

Type 'string' is not assignable to type 'DiagnosticMessage'.

Check failure on line 1656 in src/compiler/commandLineParser.ts

View workflow job for this annotation

GitHub Actions / Test Node 16 on macos-latest

Type 'string' is not assignable to type 'DiagnosticMessage'.

Check failure on line 1656 in src/compiler/commandLineParser.ts

View workflow job for this annotation

GitHub Actions / Test Node 20 on windows-latest

Type 'string' is not assignable to type 'DiagnosticMessage'.

Check failure on line 1656 in src/compiler/commandLineParser.ts

View workflow job for this annotation

GitHub Actions / Test Node 18 on windows-latest

Type 'string' is not assignable to type 'DiagnosticMessage'.

Check failure on line 1656 in src/compiler/commandLineParser.ts

View workflow job for this annotation

GitHub Actions / Test Node 16 on windows-latest

Type 'string' is not assignable to type 'DiagnosticMessage'.

Check failure on line 1656 in src/compiler/commandLineParser.ts

View workflow job for this annotation

GitHub Actions / Test Node 14 on windows-latest

Type 'string' is not assignable to type 'DiagnosticMessage'.

Check failure on line 1656 in src/compiler/commandLineParser.ts

View workflow job for this annotation

GitHub Actions / Test Node 22 on windows-latest

Type 'string' is not assignable to type 'DiagnosticMessage'.

Check failure on line 1656 in src/compiler/commandLineParser.ts

View workflow job for this annotation

GitHub Actions / browser-integration

Type 'string' is not assignable to type 'DiagnosticMessage'.

Check failure on line 1656 in src/compiler/commandLineParser.ts

View workflow job for this annotation

GitHub Actions / coverage

Type 'string' is not assignable to type 'DiagnosticMessage'.
},
];

// Do not delete this without updating the website's tsconfig generation.
Expand Down
1 change: 1 addition & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7480,6 +7480,7 @@ export interface CompilerOptions {
strictNullChecks?: boolean; // Always combine with strict property
strictPropertyInitialization?: boolean; // Always combine with strict property
strictBuiltinIteratorReturn?: boolean; // Always combine with strict property
strictSpreadCheck?: boolean; // Always combine with strict property
stripInternal?: boolean;
/** @deprecated */
suppressExcessPropertyErrors?: boolean;
Expand Down
Loading