Quasar
Quasar

Reputation: 63

TypeScript compiler not throwing error when expected in my project

I'm having a bit of trouble with a project of mine where the TypeScript compiler is not complaining about a type mismatch when I would expect it to. When I try the same block of code in the TS playground, it does complain, as I would expect. Not sure if there could be something wrong with my project configuration - I haven't noticed any other similar issues in my project.

This is the example code in question:

const func = (opt: { a: number; b: number; c: number }) => undefined;
const func2 = (arg: (opt: { a: number; b: number }) => undefined) => undefined;
func2(func);

This is the same code causing an error in the playground:

https://www.typescriptlang.org/play?ts=4.3.5#code/MYewdgzgLgBAZgVzMGBeGAKEAHKAuGAbxgEMCwEBbAIwFMAnAbhmvKrqZmDZoZgF8AlGgB8MJABNacAJZhaExgChQkWImQAmNJhL0A5gSy4CxMjAq9OrC+z5DR4sFNnyJw1GMnS5C5RuBNDADBZSA

This is my tsconfig.json:

{
  "compilerOptions": {
    "sourceMap": true,
    "noImplicitAny": true,
    "module": "es6",
    "target": "es5",
    "allowJs": false,
    "moduleResolution": "node",
    "lib": [ "es2015" ],
    "experimentalDecorators": true,
    "useDefineForClassFields": true,
    "downlevelIteration": true,
    "strictNullChecks": true,
    "strictPropertyInitialization": true,
    "outDir": "./dist/",
    "baseUrl": "./src",
    "paths": {
      "@lib/*": ["../../lib/src/*"], // relative to baseUrl
    },
    "lib": [ "dom" ],
    "jsx": "react",
  },
  "types": [
    "babylonjs"
  ],
  "typeRoots": [
    "./node_modules/@types"
  ],
  "plugins": [
    {
      "name": "typescript-plugin-css-modules"
    }
  ]
}

And I'm using TypeScript version 4.3.5 in my project.

Upvotes: 1

Views: 679

Answers (1)

You need no activate strictFunctionTypes flag.

When enabled, this flag causes functions parameters to be checked more correctly.

Also, it worth using strict: true.

The strict flag enables a wide range of type checking behavior that results in stronger guarantees of program correctness

Upvotes: 1

Related Questions