Daniel Birowsky Popeski
Daniel Birowsky Popeski

Reputation: 9286

TypeScript not checking for optional arguments compatibility

Altho I have strict: true in my tsconfig.json, this doesn't error:

const a: (b?: string) => string[] =
         (c : string) => c.split(',');
           ^
  should scream in vein

How do I get TypeScript to panic about this?

Packages versions:

Here's my complete tsconfig.json:

{
  "compilerOptions": {
    "target": "es2015",
    "module": "commonjs",
    "strict": true,
    "noImplicitAny": true,
    "allowJs": true,
    "sourceMap": true,
    "allowSyntheticDefaultImports": false,
    "moduleResolution": "node",
    "noUnusedLocals": true,
    "noUnusedParameters": false,
    "preserveConstEnums": false,
    "removeComments": false,
    "lib": [
      "es5",
      "es6",
      "dom",
      "es2015.core",
      "es2015.collection",
      "es2015.generator",
      "es2015.iterable",
      "es2015.promise",
      "es2015.proxy",
      "es2015.reflect",
      "es2015.symbol",
      "es2015.symbol.wellknown",
      "esnext.asynciterable"
    ]
  },
  "exclude": [
    "node_modules",
    "test",
    ".git"
  ]
}

Upvotes: 1

Views: 804

Answers (1)

Fenton
Fenton

Reputation: 251172

Something funky is going on in your set up. I am use TypeScript 2.6.1 with your tsconfig.json and the code:

const a: (b?: string) => any = (b: string) => 1;

Because you have the strict flag on, that includes the two flags you need to get an error; strictNullChecks and strictFunctionTypes. When I run:

tsc

I receive the message:

app.ts(1,7): error TS2322: Type '(b: string) => number' is not assignable to type '(b?: string | undefined) => any'.   Types of parameters 'b' and 'b' are incompatible.
    Type 'string | undefined' is not assignable to type 'string'.
      Type 'undefined' is not assignable to type 'string'.

How are you running the compiler? The reason I ask is that it will only use your config file if you "run it plain" as shown above. If you pass a file name argument, for example, you'll not use your config:

tsc app.ts

Results in no errors, because you aren't using your tsconfig.json any more.

Upvotes: 1

Related Questions