You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// 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.constproduction: boolean=true;interfaceIcon{x: number;y: number;name: 'we'|'you'|'hello'|undefined;}constmyBeautifulIcon: 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;
Object.assign is "just a function" so the normal widening rules apply - basically TS's assumption is that Object.assign will leave the basic type alone, but might modify the primitive within its family (e.g. maybe Object.assign({ x: -5, y: "hello"}) is { x: 5, y:" HELLO" }). as const is the correct thing to write to indicate that you don't believe that will happen.
Oo! That's a bummer.
Cause lots of times, typescript does things that "feels like" smart!
And that feeling in me made that expecttion that I wished typescript to conclude result type.
TypeScript Version: 4.0.0-dev
Search Terms: Object assign, spread
Code
(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:

The text was updated successfully, but these errors were encountered: