{"action":"created","issue":{"url":"https://api.github.com/repos/Microsoft/TypeScript/issues/7795","repository_url":"https://api.github.com/repos/Microsoft/TypeScript","labels_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/7795/labels{/name}","comments_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/7795/comments","events_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/7795/events","html_url":"https://github.com/Microsoft/TypeScript/issues/7795","id":145554584,"number":7795,"title":"Proposal: Registries","user":{"login":"Gaelan","id":4495750,"avatar_url":"https://avatars.githubusercontent.com/u/4495750?v=3","gravatar_id":"","url":"https://api.github.com/users/Gaelan","html_url":"https://github.com/Gaelan","followers_url":"https://api.github.com/users/Gaelan/followers","following_url":"https://api.github.com/users/Gaelan/following{/other_user}","gists_url":"https://api.github.com/users/Gaelan/gists{/gist_id}","starred_url":"https://api.github.com/users/Gaelan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Gaelan/subscriptions","organizations_url":"https://api.github.com/users/Gaelan/orgs","repos_url":"https://api.github.com/users/Gaelan/repos","events_url":"https://api.github.com/users/Gaelan/events{/privacy}","received_events_url":"https://api.github.com/users/Gaelan/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":6,"created_at":"2016-04-04T00:07:01Z","updated_at":"2016-04-06T04:47:19Z","closed_at":"2016-04-04T23:14:02Z","body":"Problem\r\n======\r\nMany JavaScript libraries allow you to call a function to \"register\" something using a string, then call another function with that same string to do something. For example, apps written with Meteor (a web development framework) use a \"publish/subscribe\" system where REST-based apps would use GET endpoints. Server calls `Meteor.publish` with a string and a function that returns a database cursor. The client can then call `Meteor.subscribe` with the same string (any remaining arguments are passed to the function on the server), which makes the data returned by the publish function available to the client. Currently, there is no way to ensure that the string or arguments passed to `Meteor.subscribe` are valid. This proposal aims to change that.\r\n\r\nSolution\r\n======\r\nI propose a concept of \"registries,\" which store mappings of strings (or possibly other values, but we already have a concept of strings as compile-time constants, so I am sticking with them for now) to objects. Proposed syntax: (I fully expect this to change)\r\n\r\n```typescript\r\ndeclare namespace Meteor {\r\n  registry Publications: any[];\r\n\r\n  function publish<N extends string, A extends any[], register N: A to Publications>(name: N, func: (...args: A) => Mongo.Cursor): void;\r\n\r\n  function subscribe<N extends string, A is Publications[N]>(name: N, ...args: A);\r\n}\r\n```\r\n\r\nPossible Additions\r\n------------------------\r\n- Instance-level registries for classes\r\n- Optionally only making registrations available if the code that uses them is explicitly imported\r\n  - To work for the original use case, this needs to be optional (although Meteor shares a codebase between client and server, `Meteor.publish()` is only defined on the server, so importing the file which calls it is a bad idea)"},"comment":{"url":"https://api.github.com/repos/Microsoft/TypeScript/issues/comments/206116225","html_url":"https://github.com/Microsoft/TypeScript/issues/7795#issuecomment-206116225","issue_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/7795","id":206116225,"user":{"login":"basarat","id":874898,"avatar_url":"https://avatars.githubusercontent.com/u/874898?v=3","gravatar_id":"","url":"https://api.github.com/users/basarat","html_url":"https://github.com/basarat","followers_url":"https://api.github.com/users/basarat/followers","following_url":"https://api.github.com/users/basarat/following{/other_user}","gists_url":"https://api.github.com/users/basarat/gists{/gist_id}","starred_url":"https://api.github.com/users/basarat/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/basarat/subscriptions","organizations_url":"https://api.github.com/users/basarat/orgs","repos_url":"https://api.github.com/users/basarat/repos","events_url":"https://api.github.com/users/basarat/events{/privacy}","received_events_url":"https://api.github.com/users/basarat/received_events","type":"User","site_admin":false},"created_at":"2016-04-06T04:47:19Z","updated_at":"2016-04-06T04:47:19Z","body":"This ASYNC idea was used by atom-typescript and I've cleaned up further and documented with http://alm.tools/ :rose: "}}