Lingua-e
JavaScript

What is a TS2344: Generic Constraint Violation?

← Errors

TypeScript reports this error when a type argument does not satisfy the constraints declared for a generic type parameter. Constraints are declared with 'extends' and limit which types can be used as type arguments.

Why it happens

  • Accessing a property on a generic type T without constraining T to have that property.
  • Passing a type argument that doesn't implement the required interface.
  • Using conditional types or mapped types that require specific constraints.

Examples

JavaScript

Code that triggers the error

function getLength<T>(value: T): number {
  return value.length; // T has no 'length' constraint
}

Error output

error TS2339: Property 'length' does not exist on type 'T'.

How to fix it

Add an extends constraint to the generic parameter: <T extends SomeInterface>. Narrow the constraint to the minimum shape required. If the function should accept any type, avoid accessing specific properties and use type guards instead.

Fixed code

function getLength<T extends { length: number }>(value: T): number {
  return value.length; // works for arrays, strings, etc.
}

Related errors

Ready to practice your English at work?

Lingua-e has interactive exercises built around real developer conversations: standups, code reviews, retrospectives, and more. Practice until it comes naturally.

Try Lingua-e for free