Alwaysblue
Alwaysblue

Reputation: 11950

typescript not complaining About not declaring types in constructor

I am new to typescript and I was expecting typescript to throw an error for not declaring types for my constructor but unfortunately I am not..

So I was curious why i am not getting an error.

this is what I am doing

export interface BaseConfig {
    app: express.Application, 
    routePermission: number,
    context: any
}

export class BaseConfig implements BaseConfig {
    constructor(
        context,
        authentication = false,
        authenticatedRoute = USER_TYPE.LOGGED_IN_NORMAL_USER
    ) {
        //intitalize Express App
        this.routePermission = authenticatedRoute

        this.context = context
        this.app = express()

This, is my tsconfig

{
  "compilerOptions": {
    "moduleResolution": "node",
    "experimentalDecorators": true,
    "module": "commonjs",
    "noImplicitReturns": true,
    "noUnusedLocals": true,
    "outDir": "lib",
    "sourceMap": true,
    "strict": true,
    "target": "es2017",
    "resolveJsonModule": true,
    "esModuleInterop": true,
    "noImplicitAny": false
  },
  "compileOnSave": true,
  "include": [
    "src"
  ],
  "exclude": ["node_modules"]
}

Upvotes: 1

Views: 42

Answers (2)

Valeriy Katkov
Valeriy Katkov

Reputation: 40742

authentication and authenticatedRoute parameters types are inferred from the default values, but context has any type by default, because you set noImplicitAny to false in the compilerOptions. See Compiler Options for more information about compiler flags.

Upvotes: 1

JB Nizet
JB Nizet

Reputation: 692231

noImplicitAny should be set to true. When false, it means implicit any is accepted, and variables declared without a type thus have, implicitly, the type any.

Upvotes: 2

Related Questions