Search code examples
typescripttypesanytsconfig

Typescript: Strict Checks Against "Any"


My Typescript configuration is allowing the following code to compile:

const thing : any = 123
const name : string = thing

Clearly, name is not actually a string, but the fact that I declare it as any makes my typechecker ignore it.


How can I configure my tsconfig.json to give me an error until I provide the correct types for my object?

My current configuration:

{
    "compilerOptions": {
        "module": "commonjs",
        "target": "es6",
        "noEmitOnError": true,
        "noImplicitAny": true,
        "noImplicitReturns": true,
        "noUnusedLocals": true,
        "noUnusedParameters": true,
        "strictNullChecks": true,
        "moduleResolution": "node",
        "sourceMap": true,
        "outDir": "dist",
        "baseUrl": ".",
        "paths": {
            "*": [
                "node_modules/*",
                "src/types/*"
            ]
        }
    },
    "include": [
        "src/**/*.ts"

    ]
}

Solution

  • but the fact that I declare it as any makes my typechecker ignore it.

    This is by design. By saying its any you are explicitly asking the type checker to ignore it, and that is what it does 🌹