diff --git a/packages/eslint-plugin/docs/rules/no-explicit-any.mdx b/packages/eslint-plugin/docs/rules/no-explicit-any.mdx index f0c8c20c6dec..d38997b60a54 100644 --- a/packages/eslint-plugin/docs/rules/no-explicit-any.mdx +++ b/packages/eslint-plugin/docs/rules/no-explicit-any.mdx @@ -145,6 +145,36 @@ interface Garply { } ``` +## Alternatives to `any` + +Which type(s) to use instead of `any` depends on the context. + +### Type Parameter Constraints + +"Generic" type parameters are often used to represent a value of an unknown type. +It can be tempting to use `any` as a type parameter constraint, but this is not recommended. + +First, `extends any` on its own does nothing: `` is equivalent to ``. +See [`@typescript-eslint/no-unnecessary-type-constraint`](./no-unnecessary-type-constraint.mdx) for more information. + +Within type parameters, `never` and `unknown` otherwise can generally be used instead. +For example, the following code uses those two types in `AnyFunction` instead of `any`s to constrain `Callback` to any function type: + +```ts +type AnyFunction = (...args: never[]) => unknown; + +function curry(greeter: Greeter, prefix: string) { + return (...args: Parameters) => `${prefix}: ${greeter(...args)}`; +} + +const greet = (name: string) => `Hello, ${name}!`; +const greetWithDate = curry(greet, 'Logged: '); + +greetWithDate('linter'); // => "Logged: Hello, linter!" +``` + +See [When to use `never` and `unknown` in TypeScript](https://blog.logrocket.com/when-to-use-never-unknown-typescript) for more information on those types. + ## When Not To Use It `any` is always a dangerous escape hatch. pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy