Typescript: error TS2339: Property 'startsWith' does not exist on type 'string'

Created on 19 Nov 2016  路  6Comments  路  Source: microsoft/TypeScript

tsc 2.0.10

Code

let tok = ":name";
if(tok.startsWith(":")) <--- false error: error TS2339: Property 'startsWith' does not exist on type 'string'

// A *self-contained* demonstration of the problem follows...

Expected behavior:
should know this is a method on a JS string.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith

Actual behavior:

Question

Most helpful comment

I just ran into this problem in one of my Firebase cloud functions. I can't see anything wrong with my config, you?

{
  "compilerOptions": {
    "target": "es2015",
    "module": "commonjs",
    "lib": ["es2015", "es2016", "esnext"],
    "sourceMap": true,
    "outDir": "build",
    "strict": true,
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "noImplicitThis": true,
    "alwaysStrict": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "moduleResolution": "node",
    "plugins": [
      {
        "name": "tslint-language-service"
      }
    ]
  },
  "include": ["src/**/*"]
}

All 6 comments

The method endsWith is declared into es6 (es2015). Make sure your target is es6 or higher (if target is not defined defaults to es3) or use lib option to include "es2015.core".

Compiler options.

Thanks, closing!

We have this but still get the same error

{
  "compilerOptions": {
    "target": "es2015",
    "module": "commonjs",
    "sourceMap": true
  },
  "include": [
  ],
  "exclude": [],
  "lib":[
    "es2015"
  ]
}

error TS2339: Property 'startsWith' does not exist on type 'string'.

the code:

  `if (attrKey.startsWith(IDENTIFIER) ||`

"lib" should be part of "compilerOptions" and not a sibling . see http://www.typescriptlang.org/docs/handbook/compiler-options.html and http://json.schemastore.org/tsconfig

thx

I just ran into this problem in one of my Firebase cloud functions. I can't see anything wrong with my config, you?

{
  "compilerOptions": {
    "target": "es2015",
    "module": "commonjs",
    "lib": ["es2015", "es2016", "esnext"],
    "sourceMap": true,
    "outDir": "build",
    "strict": true,
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "noImplicitThis": true,
    "alwaysStrict": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "moduleResolution": "node",
    "plugins": [
      {
        "name": "tslint-language-service"
      }
    ]
  },
  "include": ["src/**/*"]
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

zhuravlikjb picture zhuravlikjb  路  3Comments

MartynasZilinskas picture MartynasZilinskas  路  3Comments

Antony-Jones picture Antony-Jones  路  3Comments

kyasbal-1994 picture kyasbal-1994  路  3Comments

Roam-Cooper picture Roam-Cooper  路  3Comments