You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何TypeScript 2.7.2在target为es2017时将对象展开编译为Object.assign?

Why TypeScript Compiles Object Spread to Object.assign with target: "es2017"?

Great question! Let's break down exactly what's going on here:

  • Object spread in literals is an ES2018 feature, not ES6/ES2017
    A common mix-up: array spread ([...array]) was added in ES6 (ES2015), but the object spread syntax for literals ({...object}) didn't become official ECMAScript standard until ES2018. Even though it feels like a core ES6 feature, it's actually a later addition.

  • TypeScript's target option enforces version compatibility
    When you set target: "es2017" in your tsconfig.json, TypeScript guarantees that all compiled code works with ES2017 and earlier versions. Since object spread isn't part of the ES2017 spec, the compiler translates it to the equivalent ES2017-compatible code: Object.assign({}, data). This achieves the same result—copying enumerable properties from data into a new object—while staying compatible with older environments.

  • ESNext preserves cutting-edge syntax
    Using target: "ESNext" tells TypeScript to emit code using the latest ECMAScript features it supports (including newly standardized or even proposal-stage features). In your case, this means keeping the object spread syntax exactly as you wrote it, instead of downleveling it to older syntax.

For your TypeScript 2.7.2 setup, if you want to retain the object spread syntax while targeting a formal standard version, you can switch target to "es2018" instead of "es2017". TypeScript 2.7 already supports ES2018 features like object spread, so this will emit the spread syntax directly without translation.

内容的提问来源于stack exchange,提问作者jmls

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 12:00:53