问题
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?
回答1:
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]
回答2:
While compiling your typescript code, please point the target to ES6.
tsc --target ES6 "filename"
回答3:
Here : I used VS code as an IDE
Issue was :
let fName:String = "Yokey";
console.log(fName.anchor("url"));
will result in :
PS C:\MYahya\OS_DEV\typescript_lrn\1> tsc main.ts
main.ts(2,19): error TS2339: Property 'anchor' does not exist on type 'String'.
Solution :
I should include the following tsconfig.json file in the project:
{
"compilerOptions": {
"module": "commonjs",
"target": "es6",
"noImplicitAny": true,
"strictNullChecks": true,
"noImplicitReturns": true,
"noImplicitThis": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"baseUrl": "../types",
"typeRoots": [
"../types"
],
"types": [],
"forceConsistentCasingInFileNames": true,
}
}
Then I used tsc (without file name) so the transpiler would use the tsconfig.json to transcompile all type script files hosted in the directories to js files.
回答4:
if youre using intelliJ IDE like WebStorm, click on the area where your project files are on the left then search for tsconfig.json. In that file you will see your es set to an older version just change the "target": "es3" to the latest like "target": "es2018"
来源:https://stackoverflow.com/questions/34431923/error-ts2339-property-endswith-does-not-exist-on-type-string