typescript 错误 TS2339:“字符串”类型上不存在属性“endsWith”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34431923/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 03:10:22  来源:igfitidea点击:

error TS2339: Property 'endsWith' does not exist on type 'string'

angularjstypescriptecmascript-6lodash

提问by ipinak

I get this error on the code block below.

我在下面的代码块上收到此错误。

error TS2339: Property 'endsWith' does not exist on type 'string'

error TS2339: Property 'endsWith' does not exist on type 'string'

let myList = angular.element(elem).attr("href").split("/");
let last = _.last<string>(myList);
if (last.endsWith("something")) {
   return last;
}

I have also discovered this link that shows that there is a function endsWith(...).

我还发现这个链接显示有一个函数endsWith(...)

http://definitelytyped.org/docs/typescript-services--typescriptServices/classes/typescript.stringutilities.html

http://definelytyped.org/docs/typescript-services--typescriptServices/classes/typescript.stringutilities.html

Do I miss some .d.tsfile or what?

我错过了一些.d.ts文件还是什么?

回答by wandermonk

While compiling your typescript code, please point the target to ES6.

在编译打字稿代码时,请将目标指向 ES6。

tsc --target ES6 "filename"

回答by Martin Vseticka

endsWithis an ES6 functionso you need to target ES6in your TypeScript compiler settings or you can add an interface for it:

endsWith是一个ES6 函数,所以你需要ES6在你的 TypeScript 编译器设置中定位,或者你可以为它添加一个接口:

interface String {    
    endsWith(searchString: string, endPosition?: number): boolean;
};

[Playground]

[游乐场]

回答by Mohamed Yahya

Here : I used VS code as an IDE
Issue was :

这里:我使用 VS 代码作为 IDE
问题是:

let fName:String = "Yokey";
console.log(fName.anchor("url"));

will result in :

将导致:

PS C:\MYahya\OS_DEV\typescript_lrn> tsc main.ts
main.ts(2,19): error TS2339: Property 'anchor' does not exist on type 'String'.

Solution :
I should include the following tsconfig.jsonfile in the project:

解决方案:
我应该tsconfig.json在项目中包含以下文件:

{
    "compilerOptions": {
        "module": "commonjs",
        "target": "es6",
        "noImplicitAny": true,
        "strictNullChecks": true,
        "noImplicitReturns": true,
        "noImplicitThis": true,
        "noUnusedLocals": true,
        "noUnusedParameters": true,
        "baseUrl": "../types",
        "typeRoots": [
            "../types"
        ],
        "types": [],
        "forceConsistentCasingInFileNames": true,
    }
}

Then I used tsc(without file name) so the transpiler would use the tsconfig.jsonto transcompile all type script files hosted in the directories to jsfiles.

然后我使用tsc(没有文件名),所以转译器将使用 将tsconfig.json目录中托管的所有类型脚本文件转js编译为文件。

回答by Mohammad

if youre using intelliJ IDE like WebStorm, click on the area where your project files are on the left then search for tsconfig.json. In that file you will see your es set to an older version just change the "target": "es3" to the latest like "target": "es2018"

如果您使用像 WebStorm 这样的 IntelliJ IDE,请单击左侧项目文件所在的区域,然后搜索 tsconfig.json。在该文件中,您将看到您的 es 设置为旧版本,只需将 "target": "es3" 更改为最新版本,如 "target": "es2018"