Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
517 views
in Technique[技术] by (71.8m points)

ES7 Object.entries() in TypeScript not working

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

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

for ( const [ color, size ] of Object.entries(sizeByColor) ) {
    console.log(color);
    console.log(size);
}

gives the error:

TypeError: Object.entries is not a function

TypeScript v2.0.3

tsconfig.json:

{
"compilerOptions": {
    "module": "commonjs",
    "target": "es6",
    "noImplicitAny": true,
    "noEmitOnError": true,
    "outDir": "dist",
    "allowSyntheticDefaultImports": true,
    "experimentalDecorators": true,
    "pretty": true,
    "lib": [ "es2017" ],
},
"exclude": [
    "node_modules"
],
"include": [
    "./node_modules/@types/**/*.d.ts",
    "./src/**/*.ts"
]
}

I want to iterate trough object with Object.entries(), so I assigned internal definitions "lib": [ "es2017" ], but still, typescript wont allow me to transpile it.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I can reproduce your problem when I have a global compiler but not a local one in the ./node_modules.

In my case compiler just does not know which tsconfig.json file to use. Pointing it to a particular tsconfig.json file helps:

tsc  --project ./tsconfig.json

I have also added dom option to the lib, because es2017 does not recognize console:

"lib": [
    "es2017",
    "dom"
]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...