diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 67a733cbbb811..a2608a2573b13 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -39462,14 +39462,25 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { } 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 */ diff --git a/src/compiler/commandLineParser.ts b/src/compiler/commandLineParser.ts index 8248a04194666..1b4067b44a40f 100644 --- a/src/compiler/commandLineParser.ts +++ b/src/compiler/commandLineParser.ts @@ -1649,6 +1649,12 @@ const commandOptionsWithoutBuild: CommandLineOption[] = [ type: "string", defaultValueDescription: undefined, }, + { + name: "strictSpreadCheck", + type: "boolean", + default: false, + description: "Enables strict checking for object spread expressions." + }, ]; // Do not delete this without updating the website's tsconfig generation. diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 342c0f2af7146..daf9c92036395 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -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;