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

Issue

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?

Solution

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]

Answered By – Martin Vseticka

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published