This rule doesn't allow
any
types to be defined. It aims to keep TypeScript maximally useful. TypeScript has a compiler flag for--noImplicitAny
that will prevent anany
type from being implied by the compiler, but doesn't preventany
from being explicitly used.
The solution to this is not one, but two.
VSCode + ESLint should be able to check no-explicit-any
on editing
That is, if you use Node.js. There is also one for Deno.
If you have to cast to any
, consider unknown
first; otherwise, use a validation library
I prefer zod.
colinhacks / zod
TypeScript-first schema validation with static type inference
import * as z from 'zod'
// @ts-ignore
const apiKey = z.string().parse(payload.apiKey)
TypeScript has no runtime check, and type system (in IDE only) is not always fine-grained enough
- Always consider a validation library, or defensive programming
-
Hegel, or Babel with Flow + tcomb, might even work better than TypeScript. (Also, without having to use
*.ts
extension.)
Top comments (0)