constructorGenericCalls
Reports inconsistent placement of type arguments in constructor calls.
✅ This rule is included in the tsstylisticandstylisticStrictpresets.
When constructing a generic class, type arguments can be specified on either the left-hand side (as a type annotation) or the right-hand side (as part of the constructor call). This rule ensures that type arguments appear consistently on one side of the declaration.
Keeping to one side consistently improves code readability. The rule never reports when there are type arguments on both sides, or neither side of the declaration. It also doesn’t report if the names of the type annotation and the constructor don’t match.
Examples
Section titled “Examples”With the default "constructor" style:
const map: Map<string, number> = new Map();const set: Set<string> = new Set();class Example { property: Map<string, number> = new Map();}const map = new Map<string, number>();const set = new Set<string>();const map: Map<string, number> = new Map<string, number>();const map = new Map();class Example { property = new Map<string, number>();}With the "type-annotation" style:
const map = new Map<string, number>();const set = new Set<string>();const map: Map<string, number> = new Map();const set: Set<string> = new Set();const map: Map<string, number> = new Map<string, number>();const map = new Map();Options
Section titled “Options”Which side should specify the type arguments:
"constructor"(default): Type arguments should be on the constructor call."type-annotation": Type arguments should be on the type annotation.
When Not To Use It
Section titled “When Not To Use It”If your codebase prefers to keep an inconsistent preference for one style over the other, you may not need this rule. Some teams prefer to use whichever style feels more natural in each context.