Skip to content

Erroneous TS2783 when ternary expressions are inlined in a spread operatorΒ #46463

Closed
@tpict

Description

@tpict

Bug Report

πŸ”Ž Search Terms

TS2783, spread operator, control flow analysis

πŸ•— Version & Regression Information

This changed between versions 4.3.5 and 4.4.4

⏯ Playground Link

Playground link with relevant code (thanks webstrand#8856 on the TS community server)

πŸ’» Code

const override = { color: "green" };
const conditional = false as boolean;

const example = {
    color: "red",  // TS2783 here
    ...(conditional ? override : { size: "large"})
}

const more = conditional ? override : { size: "large"};
const example2 = {
    color: "red", // no TS2783 here
    ...more
}

πŸ™ Actual behavior

When using a ternary expression within a spread operator to conditionally apply properties to an object, a TS2783 error (This spread always overwrites this property) is raised even if the property is only overwritten in one branch of the conditional. The error is not present if the result of the ternary is assigned to a variable which is then applied with the spread operator.

πŸ™‚ Expected behavior

TS2783 should not be raised in situations where the property is only conditionally overwritten by a spread.

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptDesign LimitationConstraints of the existing architecture prevent this from being fixed

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions