错误 TS2339:“字符串"类型上不存在属性“endsWith" [英] error TS2339: Property 'endsWith' does not exist on type 'string'

查看:65
本文介绍了错误 TS2339:“字符串"类型上不存在属性“endsWith"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面的代码块中收到此错误.

I get this error on the code block below.

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

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

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

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

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

我是否遗漏了一些 .d.ts 文件或什么?

Do I miss some .d.ts file or what?

推荐答案

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

endsWith is an ES6 function so you need to target ES6 in your TypeScript compiler settings or you can add an interface for it:

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

[游乐场]

这篇关于错误 TS2339:“字符串"类型上不存在属性“endsWith"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆