Skip to content

Object.assign does not conclude correct type from second argumet in ternary operator #39002

Closed
@aghArdeshir

Description

@aghArdeshir

TypeScript Version: 4.0.0-dev

Search Terms: Object assign, spread

Code

// A *self-contained* demonstration of the problem follows...
// Test this by running `tsc` on the command-line, rather than through another build tool such as Gulp, Webpack, etc.
const production: boolean = true;

interface Icon {
    x: number;
    y: number;
    name: 'we' | 'you' | 'hello' | undefined;
}

const myBeautifulIcon: Icon = Object.assign(
    { x: 2, y: 2 },
    production ? { name: 'we' } : { name: 'you' }
)

console.log(myBeautifulIcon);

(code tested in nightly ts playground)

Expected behavior:
The code to be valid and myBeautifulIcon object have correct type. The correct type should be concluded from Object assign.

Actual behavior:
Error arised, saying that theconcluded type for name was string;

Playground Link:
https://www.typescriptlang.org/play/?ts=4.0.0-dev.20200609&ssl=1&ssc=1&pln=14&pc=30#code/MYewdgzgLgBADgJxAEwK7CgS3ALhgIxBABsBTAQzBgF4YoFVSBuAKBczClIQDNzhSMAJKgqAbxYwpMAB54wqALb5uraTACe8pSoRrpYcotJ4A5AHdSpmAB8YpjSFTW7pgBalixEC5iowyKQ8HKTIrAC+bKLQMIoaAEIUqFg8qMQiuMKiNDAA8vgAVqQYAHTkEBCYAOZgABSS0mKyeABMADSarTDhbQ1SiCjoWOAwAPwwTYbGZpbW4TB4k0Ym9o7O3SwAlFHgECSkJd5VtXGJ5MmYqemim0xAA

Related Issues:
#35621
#26792
#16163

More practical use case:
image

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions