typescript 打字稿告诉我属性“padStart”在类型“string”上不存在。为什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/52293010/
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
Typescript tells me property 'padStart' does not exist on type 'string'. Why?
提问by user1283776
When I type in the chrome dev tools:
当我输入 chrome 开发工具时:
"1".padStart(2,0)
I get
我得到
"01"
But when I write
但是当我写
"1".padStart(2,0)
in my typescript project
在我的打字稿项目中
I get
我得到
Property 'padStart' does not exist on type 'string'.
Why does Typescript complain? How do I fix this?
为什么打字稿抱怨?我该如何解决?
回答by Titian Cernicova-Dragomir
padStart
is defined in the ES2017
standard. You need to tell typescript to use the apropriate typings (the ones for ES2017
). You can do this either by setting the target to ES2017
, if your runtime supports all the language features required by ES2017
(which is probably not the case at the time of writing).
padStart
ES2017
标准中有定义。您需要告诉打字稿使用适当的类型(用于 的类型ES2017
)。ES2017
如果您的运行时支持 所需的所有语言功能ES2017
(在撰写本文时可能并非如此),您可以通过将目标设置为 来执行此操作。
Another option is to just set the libs
option in order to get the typings for runtime objects in accordance with ES2017
but still compile down to whatever language version you are targeting.(Note that the runtime itself needs to support padStart
, typescript will not provide any poly-fills)
另一种选择是设置该libs
选项,以便根据运行时对象获得类型,ES2017
但仍可编译为您所针对的任何语言版本。(请注意,运行时本身需要支持padStart
,打字稿不会提供任何 poly-fills )
You can do this in tsconfig
using the lib
option:
您可以tsconfig
使用以下lib
选项执行此操作:
"compilerOptions": {
"target": "es2016",
"lib": [
"es2017",
"dom"
"scripthost"
],
// ...
}
You can read more about lib vs target in this answer
您可以在此答案中阅读有关 lib 与目标的更多信息
回答by Cerberus
TypeScript assumes by default that your transpiled code will run in the worst possible environment (for now, I hope, really the worst), i.e. in browser supporting only ES5. String.prototype.padStart
is an experimental featuresupported only in ES2017, so TypeScript can't be sure that at runtime it will be here and ready.
默认情况下,TypeScript 假定您转译的代码将在最糟糕的环境中运行(目前,我希望真的是最糟糕的),即在仅支持 ES5 的浏览器中。String.prototype.padStart
是一项仅在 ES2017 中支持的实验性功能,因此 TypeScript 无法确定在运行时它会在这里并准备就绪。
If you know that this "worst case" won't appear (you aren't targeting old browsers, or are using polyfills), you can make one of the following changes to your tsconfig.json
:
如果您知道不会出现这种“最坏情况”(您不是针对旧浏览器,或者正在使用 polyfill),您可以对您的 进行以下更改之一tsconfig.json
:
Change your transpilation target. This forces your consumers to have ES2017 (or newer) runtime, but requires no more effort from yourself. In this case, set
target: "es2017"
.Change the list of libraries available. In this case, if you're not sure that your clients will be modern enough, you must provide them polyfills for the ES2017 elements you are using. In this case, add
"es2017"
to the array inlib
parameter (if it was empty, then it will belib: ["es2017"]
).
更改您的转译目标。这会强制您的消费者拥有 ES2017(或更新的)运行时,但您无需付出更多努力。在这种情况下,设置
target: "es2017"
。更改可用库列表。在这种情况下,如果您不确定您的客户端是否足够现代,您必须为您正在使用的 ES2017 元素提供 polyfill。在这种情况下,将参数添加
"es2017"
到数组中lib
(如果为空,则为lib: ["es2017"]
)。
回答by Benny Neugebauer
To make TypeScript aware of String.prototype.padStart(), you need to specify es2017
in the list of libraries in your tsconfig.json
. If you do that, then TypeScript will assume that all the ES2017 features can be used which might not be exactly what you want.
要让 TypeScript 知道String.prototype.padStart(),您需要在.prototype.padStart()es2017
的库列表中指定tsconfig.json
。如果你这样做,那么 TypeScript 会假设所有 ES2017 特性都可以使用,但这些特性可能不是你想要的。
Platforms like Node.js don't always implement the complete feature set of a new ECMAScript language specification, so it's safer to only add the language features where you are confident that they exist. If you are not sure which features are supported by your targeted platform, you can look them up on pages likes node.greenor MDNs browser compatibility list.
像 Node.js 这样的平台并不总是实现新的 ECMAScript 语言规范的完整功能集,因此只添加您确信它们存在的语言功能会更安全。如果您不确定目标平台支持哪些功能,您可以在node.green或MDNs浏览器兼容性列表等页面上查找。
The padStart
function is part of the String prototype, so it is sufficient to just add es2017.string
in your tsconfig.json
:
该padStart
功能是字符串原型的一部分,所以它足以只需添加es2017.string
在您tsconfig.json
:
{
"compilerOptions": {
"lib": ["es6", "es2017.string"],
...
},
}