Skip to content
This repository has been archived by the owner on Nov 16, 2023. It is now read-only.

Latest commit

 

History

History
69 lines (47 loc) · 1.19 KB

no-unnecessary-generics.md

File metadata and controls

69 lines (47 loc) · 1.19 KB

no-unnecessary-generics

Forbids a function to use a generic type parameter only once. Generic type parameters allow you to relate the type of one thing to another; if they are used only once, they can be replaced with their type constraint.

Bad:

function logAnything<T>(x: T): void;

Good:

function logAnything(x: any): void;

Bad:

function useLogger<T extends Logger>(logger: T): void;

Good:

function useLogger(logger: Logger): void;

Bad:

function clear<T>(array: T[]): void;

Good:

function clear(array: any[]): void;

getMeAT<T>(): T: If a type parameter does not appear in the types of any parameters, you don't really have a generic function, you just have a disguised type assertion. Prefer to use a real type assertion, e.g. getMeAT() as number. Example where a type parameter is acceptable: function id<T>(value: T): T;. Example where it is not acceptable: function parseJson<T>(json: string): T;. Exception: new Map<string, number>() is OK.

Bad:

function parse<T>(): T;
const x = parse<number>();

Good:

function parse(): {};
const x = parse() as number;