Closed
Labels
Design LimitationConstraints of the existing architecture prevent this from being fixed
Description
Code
interface BV {
A: string,
B: number,
}
type BVTemplate<K extends keyof BV> = {
[P in K]?: BV[P];
}
abstract class BaseBlock<T extends keyof BV> {
abstract readonly typeName: T;
value: BVTemplate<T>;
update(v: BV[T]) {
const updated: BVTemplate<T> = {
[this.typeName]: v,
}
// const updated: BVTemplate<T> = {};
// updated[this.typeName] = v;
this.value = updated;
}
}Expected behavior:
typeName is T, but the type is incompatible with BVTemplate because the object literal declaration stores it as string. string is not compatible to T.
Actual behavior:
I would hope this example could work. The commented string works perfectly, and in JS land those two things should be equivalent.
Metadata
Metadata
Assignees
Labels
Design LimitationConstraints of the existing architecture prevent this from being fixed
Activity
sylvanaar commentedon Jan 6, 2018
The computed property doesn't take the union type - so it is widened to string.
Simplified Example
mhegazy commentedon Jan 9, 2018
Should be addressed by #21070
sandersn commentedon Jan 9, 2018
After some discussion with Mohamed Hegazy (@mhegazy), we decided that this issue is a job for the proposed Unionize type. It is not fixed by #21070 in any case.
mhegazy commentedon Jan 9, 2018
here is the sample using the unionize type:
mhegazy commentedon Jan 9, 2018
more discussion in #18155
typescript-bot commentedon Jan 24, 2018
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.