简体   繁体   English

Karma或TypeScript编译器告诉我类型'String'上不存在属性'padStart'

[英]Karma or TypeScript compiler told me Property 'padStart' does not exist on type 'String'

I have a Angular 7 project with with lib=es2015 and target=es2015, and Angular-CLI compiling and the app running are very much OK. 我有一个Angular 7项目,其中包含lib = es2015和target = es2015,以及Angular-CLI编译和运行的应用程序非常好。 However, when running unit tests with ng test , Karma or TypeScript compiler told me Property 'padStart' does not exist on type 'String' 但是,当使用ng test运行单元测试时,Karma或TypeScript编译器告诉我类型'String'上不存在属性'padStart'

27 03 2019 10:40:57.424:INFO [karma-server]: Karma v4.0.1 server started at http://0.0.0.0:9876/
27 03 2019 10:40:57.424:INFO [launcher]: Launching browsers Chrome with concurrency unlimited
27 03 2019 10:40:57.429:INFO [launcher]: Starting browser Chrome
ERROR in node_modules/@azure/storage-blob/lib/utils/utils.common.ts(300,24): error TS2339: Property 'padStart' does not exist on type 'String'.
node_modules/@azure/storage-blob/lib/utils/utils.common.ts(301,26): error TS2339: Property 'padStart' does not exist on type 'string'.

When checking the offending codes as indicated in the error message: 检查错误消息中指示的违规代码时:

export function padStart(
  currentString: string,
  targetLength: number,
  padString: string = " "
): string {
  if (String.prototype.padStart) {
    return currentString.padStart(targetLength, padString);
  }
...

I find that the last 2 lines of codes are looking perfectly fine to me, since I understand that padStart is defined in es2017, so the programmers of the lib had crafted those. 我发现最后两行代码看起来非常好,因为我知道padStart是在es2017中定义的,所以lib的程序员已经精心设计了这些代码。 If in tsconfig.json I have lib=es2017 and target=es2015, the compiling of ng test is fine. 如果在tsconfig.json中我有lib = es2017和target = es2015,那么编译ng测试就可以了。

I like to find out the cause of the problem, and the suspects are: Type Script compiler, Karma, Angular compiler, or the offending codes. 我想找出问题的原因,并且嫌疑人是:类型脚本编译器,Karma,Angular编译器或违规代码。 Which one do you think? 你觉得哪一个?

If in tsconfig.json I have lib=es2017 and target=es2015, the compiling of ng test is fine 如果在tsconfig.json中我有lib = es2017和target = es2015,那么编译ng测试就可以了

This is the correct fix. 这是正确的解决方法。 padStart only came with ES2017 : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart padStart仅附带ES2017https//developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM