typescript “ObjectConstructor”类型上不存在属性“values”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/52304422/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Property 'values' does not exist on type 'ObjectConstructor'
提问by Sandra Willford
In the following I am converting an enum in to an array, it seems that I may have something missing in my tsconfig.json.
在下面我将一个枚举转换为一个数组,似乎我的 tsconfig.json 中可能缺少一些东西。
This is the script:
这是脚本:
const menuItems = Object.values(MediaListFilterType).map(value => ({
type: value,
description: () => {
switch (value) {
case value === MediaListFilterType.notPitched:
return 'Exclude already pitched';
break;
case value === MediaListFilterType.notDoublePitched:
return 'Exclude double pitched';
break;
case value === MediaListFilterType.assignedToMe:
return 'Assigned to me';
break;
case value === MediaListFilterType.notAssigned:
return 'Unassigned';
break;
}
}
}));
this gives me this error:
Property 'values' does not exist on type 'ObjectConstructor'.
这给了我这个错误:
Property 'values' does not exist on type 'ObjectConstructor'.
and the tsconfig is as follows...
和 tsconfig 如下...
{
"compilerOptions": {
"module": "es6",
"target": "es2015",
"sourceMap": true,
"jsx": "react",
"moduleResolution": "node",
"declaration": false,
"allowSyntheticDefaultImports": true
}
}
I am a bit new at this so I am not sure what I need to change in the tsconfig. Help!
我对此有点陌生,所以我不确定我需要在 tsconfig.xml 中更改什么。帮助!
回答by basarat
You have "target": "es2015",
but Object.values
is not a part of es2015. Its a part of es2017 : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Object/values
你有"target": "es2015",
但Object.values
不是 es2015 的一部分。它是 es2017 的一部分:https: //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Object/values
Fix
使固定
Use "target": "ESNext"
which has that feature.
使用"target": "ESNext"
具有该功能的。
回答by Charles.xue
add es2017.object
to compilerOptions lib array.
添加es2017.object
到 compilerOptions lib 数组。
回答by Hero Wanders
You are calling Object.values(...)
while targeting es2015
(also known as ES6).
Unfortunately this feature first appears in es2017
(also known as ES8), as you can see in the specifications for ES6, ES7, ES8(first appearance of Object.values
) or the MDN article.
您Object.values(...)
在定位时调用es2015
(也称为 ES6)。不幸的是,此功能首先出现在es2017
(也称为 ES8)中,正如您在ES6、ES7、ES8(首次出现Object.values
)的规范或MDN 文章 中所见。
The compiler adjusts itself to the target and reports to you that it doesn't know the function (in that version).
编译器根据目标自行调整并向您报告它不知道该函数(在该版本中)。
You may use a polyfill (as mentioned in the MDN article) or change TypeScript's target to es2017
or esnext
. If you want to let it run in a browser environment, you might have to transpile that to a lower ECMAScript version like your original target es2015
. This can be accomplished with Babel. There are many resources on the web which show you the details (and even more lately, as Babel 7 has been released with better support for TypeScript).
您可以使用 polyfill(如 MDN 文章中所述)或将 TypeScript 的目标更改为es2017
或esnext
。如果您想让它在浏览器环境中运行,您可能必须将其转换为较低的 ECMAScript 版本,例如您的原始目标es2015
。这可以通过 Babel 来完成。网络上有很多资源可以向您展示详细信息(最近发布的 Babel 7 更好地支持 TypeScript)。
By the way, this question is very similar to this ones:
顺便说一句,这个问题与这个问题非常相似: