{"action":"created","issue":{"url":"https://api.github.com/repos/Microsoft/TypeScript/issues/16541","repository_url":"https://api.github.com/repos/Microsoft/TypeScript","labels_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/16541/labels{/name}","comments_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/16541/comments","events_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/16541/events","html_url":"https://github.com/Microsoft/TypeScript/issues/16541","id":236055154,"number":16541,"title":"Type-based flow control","user":{"login":"kol-93","id":1623880,"avatar_url":"https://avatars0.githubusercontent.com/u/1623880?v=3","gravatar_id":"","url":"https://api.github.com/users/kol-93","html_url":"https://github.com/kol-93","followers_url":"https://api.github.com/users/kol-93/followers","following_url":"https://api.github.com/users/kol-93/following{/other_user}","gists_url":"https://api.github.com/users/kol-93/gists{/gist_id}","starred_url":"https://api.github.com/users/kol-93/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kol-93/subscriptions","organizations_url":"https://api.github.com/users/kol-93/orgs","repos_url":"https://api.github.com/users/kol-93/repos","events_url":"https://api.github.com/users/kol-93/events{/privacy}","received_events_url":"https://api.github.com/users/kol-93/received_events","type":"User","site_admin":false},"labels":[{"id":117630923,"url":"https://api.github.com/repos/Microsoft/TypeScript/labels/Out%20of%20Scope","name":"Out of Scope","color":"556677","default":false}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":2,"created_at":"2017-06-15T01:48:10Z","updated_at":"2017-06-15T17:59:25Z","closed_at":null,"body":"<!-- SUGGESTIONS: See https://github.com/Microsoft/TypeScript-wiki/blob/master/Writing-Good-Design-Proposals.md -->\r\n**SUGGESTION**\r\nCompilation-time type-based flow control in overloaded functions and/or type-based generic specializations and/or safe static castings are NEEDED.\r\n\r\nSuggested syntax:\r\n * Compilation-time type-based flow control: [C++ compile-time](http://en.cppreference.com/w/cpp/language/overload_resolution)\r\n * Compilation-time generic specializations: [C++ compile-time](http://en.cppreference.com/w/cpp/language/template_specialization)\r\n * Safe static castings: static_cast<TargetType>(x: object) => TargetType | void \r\n\r\n**TypeScript Version:**  2.4.0 / nightly (2.5.0-dev.201xxxxx)\r\n\r\n**Code**\r\n**Example that shows inconsistency of current language abilities**\r\n```\r\ninterface IFirst { \r\n\tf1(): void;\r\n}\r\n\r\ninterface ISecond { \r\n\tf2(): void; \r\n}\r\n\r\nfunction isFirst(obj: object) {\r\n\treturn typeof (obj as IFirst).f1 == 'function';\r\n}\r\n\r\nfunction isSecond(obj: object) {\r\n\treturn typeof (obj as ISecond).f2 == 'function';\r\n}\r\n\r\n\r\n\r\nclass CorrectImplementation implements IFirst, ISecond {\r\n\tf1() {\r\n\t\tconsole.info('+f1');\r\n\t}\r\n\t\r\n\tf2() {\r\n\t\tconsole.info('+f2');\r\n\t}\r\n\r\n}\r\n\r\ninterface IPerson {\r\n\tname: string;\r\n}\r\n\r\nclass IncorrectImplementation implements ISecond {\r\n\tf1(x: IPerson): void {\r\n\t\tconsole.info(`f1(person: ${JSON.stringify(x.name)})`);\r\n\t}\r\n\t\r\n\tf2() {\r\n\t\tconsole.info('+f2');\r\n\t}\r\n\r\n}\r\n\r\nlet objects:Array<IFirst | ISecond> = [\r\n\tnew CorrectImplementation(),\r\n\tnew IncorrectImplementation()\r\n];\r\n\r\nfor (let inst of objects) {\r\n\tif (isFirst(inst)) { ///\r\n\t\t// invalid function type for IncorrectImplementation: (x: IPerson) => void\r\n\t\t// expected () => void\r\n\t\t// => TypeError (runtime): Cannot read property 'name' of undefined\r\n\t\t(inst as IFirst).f1();\r\n\t}\r\n\tif (isSecond(inst)) {\r\n\t\t(inst as ISecond).f2();\r\n\t}\r\n}\r\n\r\n```\r\n**Summary:**\r\n* Type guards do not work as needed.\r\n* There is no type-based flow control.\r\n* 'Compilation-time type checks' does not wor as expected\r\n"},"comment":{"url":"https://api.github.com/repos/Microsoft/TypeScript/issues/comments/308821046","html_url":"https://github.com/Microsoft/TypeScript/issues/16541#issuecomment-308821046","issue_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/16541","id":308821046,"user":{"login":"kol-93","id":1623880,"avatar_url":"https://avatars0.githubusercontent.com/u/1623880?v=3","gravatar_id":"","url":"https://api.github.com/users/kol-93","html_url":"https://github.com/kol-93","followers_url":"https://api.github.com/users/kol-93/followers","following_url":"https://api.github.com/users/kol-93/following{/other_user}","gists_url":"https://api.github.com/users/kol-93/gists{/gist_id}","starred_url":"https://api.github.com/users/kol-93/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kol-93/subscriptions","organizations_url":"https://api.github.com/users/kol-93/orgs","repos_url":"https://api.github.com/users/kol-93/repos","events_url":"https://api.github.com/users/kol-93/events{/privacy}","received_events_url":"https://api.github.com/users/kol-93/received_events","type":"User","site_admin":false},"created_at":"2017-06-15T17:59:25Z","updated_at":"2017-06-15T17:59:25Z","body":"I only want to write something like\r\n```\r\nfor (let inst of objects) {\r\n\tif (is_base_of<IFirst, typeof inst)>.value) { ///\r\n\t\t(inst as IFirst).f1();\r\n\t}\r\n\tif (is_base_of<ISecond, typeof inst)>.value) {\r\n\t\t(inst as ISecond).f2();\r\n\t}\r\n}\r\n```\r\nwithout any type guards and other spikes. It's only compilation-time type-based flow.\r\nTypeScript has no good implementation of overloaded functions (because of single implementation for many types). It also has no good implementation for generics for the same reasons. But if you want to make strong language for compilation-time process you ** HAVE TO MAKE ** theese things."}}
{"actor":{"display_login":"kol-93"}}