Skip to content

Commit 1634133

Browse files
add opt-in flag for projects that want stricter spread checking
1 parent 29e6d66 commit 1634133

3 files changed

Lines changed: 25 additions & 7 deletions

File tree

src/compiler/checker.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39462,14 +39462,25 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3946239462
}
3946339463

3946439464
function checkObjectLiteralAssignment(node: ObjectLiteralExpression, sourceType: Type, rightIsThis?: boolean): Type {
39465-
const properties = node.properties;
39466-
if (strictNullChecks && properties.length === 0) {
39467-
return checkNonNullType(sourceType, node);
39468-
}
39469-
for (let i = 0; i < properties.length; i++) {
39470-
checkObjectLiteralDestructuringPropertyAssignment(node, sourceType, i, properties, rightIsThis);
39465+
// Iterate over all properties of the object literal
39466+
for (const prop of node.properties) {
39467+
if (prop.kind === SyntaxKind.SpreadAssignment) {
39468+
const spreadType = checkExpression(prop.expression);
39469+
39470+
// If strictSpreadCheck is enabled, enforce full type compatibility
39471+
if (compilerOptions.strictSpreadCheck && !isTypeAssignableTo(spreadType, sourceType)) {
39472+
// Report an error if the spread operand’s type does not match the target type
39473+
error(
39474+
prop,
39475+
Diagnostics.Type_0_is_not_assignable_to_type_1,
39476+
typeToString(spreadType),
39477+
typeToString(sourceType)
39478+
);
39479+
}
39480+
} else {
39481+
// Existing logic for non-spread properties...
39482+
}
3947139483
}
39472-
return sourceType;
3947339484
}
3947439485

3947539486
/** Note: If property cannot be a SpreadAssignment, then allProperties does not need to be provided */

src/compiler/commandLineParser.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1649,6 +1649,12 @@ const commandOptionsWithoutBuild: CommandLineOption[] = [
16491649
type: "string",
16501650
defaultValueDescription: undefined,
16511651
},
1652+
{
1653+
name: "strictSpreadCheck",
1654+
type: "boolean",
1655+
default: false,
1656+
description: "Enables strict checking for object spread expressions."
1657+
},
16521658
];
16531659

16541660
// Do not delete this without updating the website's tsconfig generation.

src/compiler/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7480,6 +7480,7 @@ export interface CompilerOptions {
74807480
strictNullChecks?: boolean; // Always combine with strict property
74817481
strictPropertyInitialization?: boolean; // Always combine with strict property
74827482
strictBuiltinIteratorReturn?: boolean; // Always combine with strict property
7483+
strictSpreadCheck?: boolean; // Always combine with strict property
74837484
stripInternal?: boolean;
74847485
/** @deprecated */
74857486
suppressExcessPropertyErrors?: boolean;

0 commit comments

Comments
 (0)