TypeScript Class Instantiation scenarios When Extended with Generic Type Constraints
I need help solving I'm working with an scenario with TypeScript where my class instantiation fails due to the generic type constraints I've implemented. I have a base class `Box<T>` that takes a generic type, and a derived class `ColoredBox<T>` that extends `Box<T>`. However, when I try to create an instance of `ColoredBox`, I get a compilation behavior that says `Type 'T' does not satisfy the constraint 'string'`. The code looks like this: ```typescript class Box<T> { value: T; constructor(value: T) { this.value = value; } } class ColoredBox<T extends string> extends Box<T> { color: string; constructor(value: T, color: string) { super(value); this.color = color; } } const myBox = new ColoredBox(123, 'red'); // This line causes a compilation behavior ``` I've ensured that the type being passed to `ColoredBox` is indeed a string, but TypeScript seems to infer `T` as `number` instead. I also tried explicitly specifying the type when creating the instance: ```typescript const myBox = new ColoredBox<string>('123', 'red'); ``` But I still encounter the same scenario. I suspect this has something to do with how TypeScript infers types in generics across class hierarchies. Any insights on why this is happening and how I can resolve it? I'm currently using TypeScript version 4.5.4. I'm working on a API that needs to handle this. What's the best practice here? I'm on CentOS using the latest version of Typescript. My development environment is CentOS.