how do you represent the equivalent of java’s mytype? extends myconstraint in typescript without using any?

The equivilent in Typescript is actually very similar to what you referenced with Java.

type Foo<T extends MyConstraint> = Array<MyClass<T>>;

This would create an array type where the instances of MyClass have their parameterized generic argument constrained to anything that is a sub-type of MyConstraint just as you had done with Java.

This can come in different flavors depending on what you’re doing (creating a type, defining a generic class, defining a generic function, etc) but the basic syntax is <T extends SomeConstraint>.

Here’s the relevant Typescript docs talking about generic constraints which covers all the details: https://www.typescriptlang.org/docs/handbook/2/generics.html#generic-constraints

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top