Skip to content

constructorGenericCalls

Reports inconsistent placement of type arguments in constructor calls.

✅ This rule is included in the ts stylistic presets.

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.

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();
}

With the "type-annotation" style:

const map = new Map<string, number>();
const set = new Set<string>();

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.

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.

Made with ❤️‍🔥 in Boston by Josh Goldberg and contributors.