TS stating that test() is not a property on a string?

Viewed 2663

I have a simple RegEx pattern test that I'd like to run on a string; you can see this in the example below as the variable part.

part is a string

When I do this, however, the red-squiggly line is indicating the error of:

Property 'test' does not exist on type 'string'.ts(2339)

I am using TS 3.2.2 and my tsconfig.json is rather unexciting:

{
  "compilerOptions": {
    "declaration": true,
    "module": "commonjs",
    "moduleResolution": "node",
    "target": "es2017",
    "noImplicitAny": true,
    "outDir": "./lib",
    "preserveConstEnums": true,
    "removeComments": true,
    "typeRoots": ["./node_modules/@types"]
  },
  "include": ["src/**/*.ts"],
  "exclude": ["node_modules", "**/*-spec.ts"]
}

Can anyone help me understand this? Clearly test IS a property on string!

1 Answers

Put it on chrome console and you will get this: enter image description here but you can do this:

if( /\s+-\s+-[0-9]/.test(part) ) return part;
Related