any keyword¶
Rule¶
NEVER use any keyword, prefer using unknown if you don’t know the type in advance.
- Type safety loss - Using any effectively opts out of TypeScript's type checking for that variable or function, negating the main benefit of using TypeScript in the first place.
- IDE support reduction - You lose intelligent code completion, refactoring tools, and inline documentation that TypeScript-aware IDEs provide.
- Bug introduction risk - any can silently allow type errors that would otherwise be caught during development, leading to runtime errors.
- Code maintenance challenges - Future developers (including yourself) won't understand what data structure is expected, making the code harder to maintain.
- Refactoring difficulties - When refactoring, TypeScript can't help identify where changes are needed for any-typed variables.
Integration¶
You can add this rule in your tsconfig.json file to ensure there is no any in your code :
Examples¶
🚨 DON’T¶
const format = (date: Date, displayFormat: any): string => {
const day = date.getDate();
const month = date.getMonth() + 1;
const year = date.getFullYear();
return `${this.to2digit(day)}/${this.to2digit(month)}/${year}`;
};
type Adapter<T> = {
adapt(item: any): T;
};
const countryString: any = this.convertSpecificCountryCodes(country);
const warehouseSession: any = {
warehouseId: warehouse.warehouseId,
shortWarehouseId: warehouse.shortWarehouseId,
warehouseName: warehouse.warehouseName,
address: warehouse.address,
language: warehouseLanguage,
};
✅ DO
const format = (date: Date, displayFormat: string): string => {
const day = date.getDate();
const month = date.getMonth() + 1;
const year = date.getFullYear();
return `${this.to2digit(day)}/${this.to2digit(month)}/${year}`;
};
type Adapter<T> = {
adapt(item: unknown): T;
};
const countryString: string = this.convertSpecificCountryCodes(country);
const warehouseSession: WarehouseSession = {
warehouseId: warehouse.warehouseId,
shortWarehouseId: warehouse.shortWarehouseId,
warehouseName: warehouse.warehouseName,
address: warehouse.address,
language: warehouseLanguage,
};