Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
zlepper opened this issue Nov 13, 2017 · 2 comments
Closed

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

zlepper opened this issue Nov 13, 2017 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@zlepper
Copy link

zlepper commented Nov 13, 2017

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
    }
}
@mhegazy
Copy link
Contributor

mhegazy commented Nov 13, 2017

Duplicate of #5579

@mhegazy mhegazy marked this as a duplicate of #5579 Nov 13, 2017
@mhegazy mhegazy added the Duplicate An existing issue was already created label Nov 13, 2017
@zlepper
Copy link
Author

zlepper commented Nov 13, 2017

Seems like it, couldn't find that issue, will close this one.

@zlepper zlepper closed this as completed Nov 13, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants