错误TS2339:房产“的endsWith”在类型'字符串'不存在 [英] error TS2339: Property 'endsWith' does not exist on type 'string'

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

问题描述

我得到这个错误低于code块上。

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(...)

<一个href=\"http://definitelytyped.org/docs/typescript-services--typescriptServices/classes/typescript.stringutilities.html\" rel=\"nofollow\">http://definitelytyped.org/docs/typescript-services--typescriptServices/classes/typescript.stringutilities.html

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

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

推荐答案

的endsWith 是的 ES6功能,所以你需要针对 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;
};

[<一个href=\"http://www.typescriptlang.org/Playground#src=interface%20String%20%7B%20%20%20%20%0D%0A%20%20%20%20endsWith(searchString%3A%20string%2C%20endPosition%3F%3A%20number)%3A%20boolean%3B%0D%0A%7D%3B%0D%0A%0D%0Alet%20test%20%3D%20%22lala%22%3B%0D%0A%0D%0Aif%20(test.endsWith(%22la%22))%20%7B%0D%0A%09console.log(%22OK!%22)%3B%0D%0A%7D\"相对=nofollow>游乐场]

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

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