Consider the following scenario:
interface Test {
inner: {
value: boolean,
}
}
We also have a class defined as:
class ContextualData<T> {
constructor(public data: T) {}
}
The objective is to wrap the 'value' property in 'inner' object of 'Test' interface inside a ContextualData object like this:
const original: Test = {
inner: {
value: true,
},
}
// Wrap the value in a ContextualData object.
original.inner.value = new ContextualData<boolean>(original.inner.value)
To achieve this, certain types are declared:
export type Primitive = undefined | null | boolean | string | number | Function
export type Contextuable<T> = T | ContextualData<T>
export type DeepContextuable<T> =
T extends Primitive ? Contextuable<T> : DeepContextuableObject<T>
export type DeepContextuableObject<T> = {
[K in keyof T]: DeepContextuable<T[K]>
}
DeepContextual type is then used to transform the Test interface:
const original: DeepContextual<Test> = {
inner: {
value: new ContextualData<boolean>(true),
},
}
After adding a new 'map' method to the ContextualData class:
class ContextualData<T> {
constructor(public data: T) {}
public map<U>(mapFn: (current: T) => U): U {
return mapFn(this.data)
}
}
Even without using the 'map' function, assigning 'value: ContextualData<boolean>(true)' throws a TypeScript error:
TS2322: Type 'ContextualData<boolean>' is not assignable to type
'boolean | ContextualData<true> | ContextualData<false>'.
What could be the issue here? Is it possibly a bug?