DEV Community

Pacharapol Withayasakpunt
Pacharapol Withayasakpunt

Posted on

5 3

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 colinhacks / 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

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay