Christopher Caldwell
Christopher Caldwell

Reputation: 154

How to prevent "any" from being used in TypeScript types and interfaces

I have the "noImplicitAny": true, flag in tsconfig.json, and "@typescript-eslint/no-explicit-any": 2, in place for eslint, but they don't catch things like

type BadInterface {
  property: any
}

Are there any configuration options through tsconfig, or eslint to flag this? Our team's contractors use any for lots of properties in interfaces, and I can't seem to find a way to prevent this from passing tsc or eslint commands in the CI workflow.

To just be completely clear, I want something to let me know when someone uses type any for a key on an interface or type. It seems to be fine when TS is inferring the type from an any, but I want it blow up when someone tries to use any as a type anywhere.

Upvotes: 2

Views: 1783

Answers (1)

SMHutch
SMHutch

Reputation: 126

I believe you can use the ban-types rule to prevent explicit use of any.

For example:

"@typescript-eslint/ban-types": [
  2,
  {
    "types": {
      "any": "Don't use 'any' because it is unsafe",
    },
    "extendDefaults": true,
  }
}

Docs

Upvotes: 3

Related Questions