TypeScript
ts/no-explicit-any
Type values with a real type or unknown (then narrow), never any.
umiarkowanastatycznefe-typescript
Dlaczego
any switches type checking off for the value and for everything it flows into: a typo in a property name, a missing await or a wrong argument all compile and fail at run time. One any in an API helper spreads to every component that uses its result.
Poprawka
Use the real type, a generic, or unknown and narrow it before use (typeof, instanceof, in, a type guard, or a schema validator at the boundary). In catch (err), leave the type off: with strict it is unknown. For a third-party gap, write a small declaration instead of any.
Przykłady
// bad
function total(items: any) { return items.reduce((sum: any, i: any) => sum + i.price, 0) }
// good
function total(items: { price: number }[]) { return items.reduce((sum, i) => sum + i.price, 0) }Źródła
Fakty
- Identyfikator
ts/no-explicit-any- Waga
- umiarkowana
- Sprawdzanie
- statyczne sprawdzane w kodzie źródłowym przez
audit - Dotyczy plików
jsxvuesvelteastro- Tylko w projektach z
typescript- Uczy jej skill
fe-typescriptTypeScript- Od wersji
- 0.1.0
Przeczytaj w terminalu
npx github:dawidolko/frontend-skills explain ts/no-explicit-any