Skip to content

Object literal with variable property is overly generic #21030

Closed
@xealot

Description

@xealot

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

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions