Skip to content

Use const as key of computed property in interface map #19960

Closed
@zlepper

Description

@zlepper

For context: As part of out current codebase we have some model classes that are dynamically registered with our modelFactory, which can fetch the models from our backend.

I just discovered the lovely type maps, and have a slight idea for an improvement.

Our code follows something like this:

const modelName = "foo";

export class FooModel {
   ...
}

modelFactory.register(modelName, FooModel);

declare module 'modelFactory' {
    interface ModelMap {
        "foo": FooModel
    }
}

I would like to be able to declare the "foo" key in the map using the previous constant modelName, to ensure the names doesn't get out of sync. Currently that fails with 'TS1169: A computed property name in an interface must directly refer to a built-in symbol.', which i can understand, as the value could in theory change. However in this case we are dealing with a constant, which should be safe to evaluate on at compile time.

So i short i would like to have the following code instead:

const modelName = "foo";

export class FooModel {
   ...
}

modelFactory.register(modelName, FooModel);

declare module 'modelFactory' {
    interface ModelMap {
        [modelName]: FooModel
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions