loading...

TypeScript with ESLint no-explicit-any

patarapolw profile image Pacharapol Withayasakpunt ・1 min read

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)

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

Posted on by:

patarapolw profile

Pacharapol Withayasakpunt

@patarapolw

Currently interested in TypeScript, Vue, Kotlin and Python. Looking forward to learning DevOps, though.

Discussion

markdown guide