Vyhledávání na webu

angularjs - chyba TS2339: Vlastnost 'endsWith' neexistuje u typu 'string'


Na následujícím bloku kódu dostávám tuto chybu.

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

Také jsem objevil tento odkaz, který ukazuje, že existuje funkce endsWith(...).

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

Chybí mi nějaký soubor .d.ts nebo co?

Odpověď č. 1

Při kompilaci vašeho TypeScript kódu prosím nastavte cílovou verzi na ES6.

tsc --target ES6 "filename"

Odpověď č. 2

endsWith je funkce ES6, takže musíte v nastavení kompilátoru TypeScript cílit na ES6, nebo si můžete přidat pro ni rozhraní:

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

[Hřiště]

Související články