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

A computed property name in an interface must directly refer to a built-in symbol #13190

Closed
nbransby opened this issue Dec 28, 2016 · 7 comments
Labels
Duplicate An existing issue was already created

Comments

@nbransby
Copy link

TypeScript Version: 2.1

When will TypeScript be able to type check this:

const location = Symbol("Quick reply location")
const onText = Symbol("Text message responder")
const onLocation = Symbol("Location message responder")
const onImage = Symbol("Image message responder")

interface ResponseHandler {
    [quickReply: string]: () => void
    [location](lat: number, long: number): void
    [onText](text: string): void;
    [onLocation](lat: number, long: number): void;
    [onImage](url: string): void;
}

What I would like it to do is verify the methods defined on an object conforming to this interface have the right parameters based on the property symbol / type (in case of string)

@aluanhaddad
Copy link
Contributor

aluanhaddad commented Dec 28, 2016

How would an implementation acquire the symbol for say, location?

@nbransby
Copy link
Author

let handler: ResponseHandler = {
        [location](lat: number, long: number) {
            console.log(`User located at ${lat}, ${long}`);            
        }
    }

@aluanhaddad
Copy link
Contributor

And where does that symbol come from? Is it something exported by the module that exports the interface?

@nbransby
Copy link
Author

Yes it is defined above the interface in the example

@mhegazy
Copy link
Contributor

mhegazy commented Dec 28, 2016

Duplicate of #5579

@mhegazy mhegazy added the Duplicate An existing issue was already created label Dec 28, 2016
@aluanhaddad
Copy link
Contributor

@nbransby right, of course! for some reason I thought you were using modules. I don't know why, as you do not in your example.
Regardless, since each instantiation of a non-well-known symbol is unique, it would make it impossible for consumers to implement the interface without acquiring the original instantiation. Perhaps that is desirable in your use case though.

@nbransby
Copy link
Author

Yes thats exactly the use case im after. My example is non-module based for simplicity but the symbols would be exported with the interface

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 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

4 participants