ES7 Object.entries() in TypeScript not working

前端 未结 4 2198
鱼传尺愫
鱼传尺愫 2020-12-01 20:39

I have an issue with transpiling ES7 code with TypeScript. This code:

const sizeByColor = {
    red: 100,
    green: 500,
};

for ( const [ color, size ] of          


        
4条回答
  •  日久生厌
    2020-12-01 21:15

    If you don't want to include the full set of ECMAScript 2017 (ES8) in your set of libraries, then you can also use es2017.object to just satisfy the resolution of Object.entries and related.

    Here is a minimal working example:

    src/index.ts

    const sizeByColor = {
      red: 100,
      green: 500,
    };
    
    for (const [color, size] of Object.entries(sizeByColor)) {
      console.log(color);
      console.log(size);
    }
    

    tsconfig.json

    {
      "compilerOptions": {
        "lib": ["dom", "es2016", "es2017.object"],
        "rootDir": "src",
        "target": "es6",
        "outDir": "dist"
      },
      "exclude": [
        "node_modules"
      ]
    }
    

    Note: If "target" is set to "es6", then TypeScript uses by default the following "lib" entries (if none are specified): ["dom", "dom.iterable", "es6", "scripthost"].

    The library defaults get overwritten when setting "lib" manually, that's why I needed to add "dom" (to use console.log in my code) and "es6" to demonstrate the usage of ES6 and partial ES8 ("es2017.object").

    Source: TypeScript Compiler Options

提交回复
热议问题