{"action":"created","issue":{"url":"https://api.github.com/repos/Microsoft/TypeScript/issues/5579","repository_url":"https://api.github.com/repos/Microsoft/TypeScript","labels_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/5579/labels{/name}","comments_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/5579/comments","events_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/5579/events","html_url":"https://github.com/Microsoft/TypeScript/issues/5579","id":115994355,"number":5579,"title":"Support type-checking of computed properties for constants and Symbols","user":{"login":"IgorMinar","id":216296,"avatar_url":"https://avatars.githubusercontent.com/u/216296?v=3","gravatar_id":"","url":"https://api.github.com/users/IgorMinar","html_url":"https://github.com/IgorMinar","followers_url":"https://api.github.com/users/IgorMinar/followers","following_url":"https://api.github.com/users/IgorMinar/following{/other_user}","gists_url":"https://api.github.com/users/IgorMinar/gists{/gist_id}","starred_url":"https://api.github.com/users/IgorMinar/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/IgorMinar/subscriptions","organizations_url":"https://api.github.com/users/IgorMinar/orgs","repos_url":"https://api.github.com/users/IgorMinar/repos","events_url":"https://api.github.com/users/IgorMinar/events{/privacy}","received_events_url":"https://api.github.com/users/IgorMinar/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/Microsoft/TypeScript/labels/In%20Discussion","name":"In Discussion","color":"556b75"},{"url":"https://api.github.com/repos/Microsoft/TypeScript/labels/Suggestion","name":"Suggestion","color":"006b75"}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":10,"created_at":"2015-11-09T23:52:04Z","updated_at":"2016-08-19T15:59:44Z","closed_at":null,"body":"On Angular we are considering using ES6 computed prototype methods as part of Angular’s component api but we realized that typescript doesn’t allow us to express this in a type-safe way.\r\n\r\nI’m wondering if this is a feature that could be added to typescript or if there is a fundamental issue that would prevent typescript from supporting this. Example:\r\n\r\n```\r\n{Component, onInit, OnInit} from ‘angular2/core';\r\n\r\n@Component({\r\n  selector: 'my-component'\r\n})\r\nclass MyComponent implements OnInit {\r\n  [onInit](param1, param2){ return something; }\r\n}\r\n```\r\n\r\nWhere `onInit` is a const string or ES Symbol, and `OnInit` is an interface with the signature of the `[onInit]` method. `onInit` is a an optional life-cycle hook that Angular will call if the component has this property name.\r\n\r\nThe reason why we find this api style attractive is that it removes the possibility of name-collisions, which means that we are free to add support for more hooks in the future without affecting any existing components.\r\n\r\nPS: ES Observables are already using Symbols to define the observable interface, so this feature would allow creation of Observable interface. I expect more libs will want to take advantage of this api style as computed property and Symbol support becomes better.\r\n\r\nRelated issue: #4653"},"comment":{"url":"https://api.github.com/repos/Microsoft/TypeScript/issues/comments/241058661","html_url":"https://github.com/Microsoft/TypeScript/issues/5579#issuecomment-241058661","issue_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/5579","id":241058661,"user":{"login":"tomdale","id":90888,"avatar_url":"https://avatars.githubusercontent.com/u/90888?v=3","gravatar_id":"","url":"https://api.github.com/users/tomdale","html_url":"https://github.com/tomdale","followers_url":"https://api.github.com/users/tomdale/followers","following_url":"https://api.github.com/users/tomdale/following{/other_user}","gists_url":"https://api.github.com/users/tomdale/gists{/gist_id}","starred_url":"https://api.github.com/users/tomdale/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tomdale/subscriptions","organizations_url":"https://api.github.com/users/tomdale/orgs","repos_url":"https://api.github.com/users/tomdale/repos","events_url":"https://api.github.com/users/tomdale/events{/privacy}","received_events_url":"https://api.github.com/users/tomdale/received_events","type":"User","site_admin":false},"created_at":"2016-08-19T15:59:44Z","updated_at":"2016-08-19T15:59:44Z","body":"I am running into this limitation in my current project. Specifically, I am implementing a JSON serializer that needs to retrieve information about models, such as their ID, attributes, relationships, etc.\r\n\r\nInstead of hard-coding the serializer to a particular model library, I wanted to support a serialization protocol object that any model instance can implement, using symbols. Unfortunately the inability to define an interface using a non-built-in `Symbol` is stymying that somewhat.\r\n\r\nThe code looks something like this:\r\n\r\n```typescript\r\nnamespace JSONAPISerializer {\r\n  export const PROTOCOL = Symbol(\"serializer-protocol\");\r\n\r\n  export interface SerializerProtocol {\r\n    getType(): string;\r\n    getID(): string | number;\r\n  }\r\n\r\n  export interface Serializable {\r\n    [PROTOCOL]: SerializerProtocol;\r\n  }\r\n}\r\n```\r\n\r\nI want to be able to write a `Model` class that implements the serialization protocol:\r\n\r\n```typescript\r\nimport { PROTOCOL, Serializable } from \"json-serializer\";\r\n\r\nclass Model implements Serializable {\r\n  [PROTOCOL]: {\r\n    getType() { ... },\r\n    getId(): { ... }\r\n  }\r\n}\r\n```\r\n\r\nIn my case, I am happy to have the constraint of the symbol being `const`—redefining symbols at runtime seems like a bad idea anyway."}}
{"actor":{"display_login":"tomdale"}}