DEV Community

Pacharapol Withayasakpunt
Pacharapol Withayasakpunt

Posted on

TypeScript with ESLint no-explicit-any

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 an any type from being implied by the compiler, but doesn't prevent any from being explicitly used.

https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/no-explicit-any.md

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.

GitHub logo vriad / zod

TypeScript-first schema validation with static type inference


import * as z from 'zod'

// @ts-ignore
const apiKey = z.string().parse(payload.apiKey)
Enter fullscreen mode Exit fullscreen mode

TypeScript has no runtime check, and type system (in IDE only) is not always fine-grained enough

Top comments (0)