错误TS2339:属性'endsWith'不存在于类型'string' [英] error TS2339: Property 'endsWith' does not exist on type 'string'

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

问题描述



错误TS2339:属性'endsWith'不存在于类型'string'

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

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



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



我想念一些 .d.ts 文件或什么?

解决方案

endsWith 是一个 ES6功能,因此您需要在TypeScript编译器设置中定位 ES6 ,或者可以添加界面为此:

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

[游乐场]


I get this error on the code block below.

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

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

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

解决方案

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;
};

[Playground]

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

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