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

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay