TypeScript Error TS2322 when using a mapped type with conditional properties in a class
I'm encountering a TypeScript error (TS2322) when trying to define a class that utilizes a mapped type with conditional properties. My goal is to create a class that has a property `data` which varies its type based on the `type` property. Here's a simplified version of what I have: ```typescript type DataType = 'string' | 'number'; type DataMap<T extends DataType> = { [K in T]: K extends 'string' ? string : number; }; class DataContainer<T extends DataType> { type: T; data: DataMap<T>[T]; constructor(type: T, data: DataMap<T>[T]) { this.type = type; this.data = data; } } const stringData = new DataContainer('string', 'Hello World'); const numberData = new DataContainer('number', 42); ``` However, when I attempt to instantiate `DataContainer`, I get the following error: ``` Error TS2322: Type 'string' is not assignable to type 'DataMap<T>[T]'. ``` I tried to adjust the type of the `data` property and even created a separate interface for managing the data types, but it hasn't resolved the issue. The error message indicates that TypeScript isn't correctly inferring the type for `data` based on the value of `type`. My TypeScript version is 4.5.4. Is there a way to properly structure the class to avoid this type error? Or am I misunderstanding how mapped types and generics should work together in this context?