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

ipi*_*nak 9 angularjs typescript ecmascript-6 lodash

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

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;
}
Run Code Online (Sandbox Code Playgroud)

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

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

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

Mar*_*cka 14

endsWithES6函数,因此您需要ES6在TypeScript编译器设置中进行定位,或者可以为其添加接口:

interface String {    
    endsWith(searchString: string, endPosition?: number): boolean;
};
Run Code Online (Sandbox Code Playgroud)

[ 游乐场 ]


wan*_*onk 14

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

tsc --target ES6 "filename"
Run Code Online (Sandbox Code Playgroud)