Title: Support user-defined type guard functions · Issue #1007 · microsoft/TypeScript · GitHub
Open Graph Title: Support user-defined type guard functions · Issue #1007 · microsoft/TypeScript
X Title: Support user-defined type guard functions · Issue #1007 · microsoft/TypeScript
Description: We currently only support some baked-in forms of type checking for type guards -- typeof and instanceof. In many cases, users will have their own functions that can provide run-time type information. Proposal: a new syntax, valid only in...
Open Graph Description: We currently only support some baked-in forms of type checking for type guards -- typeof and instanceof. In many cases, users will have their own functions that can provide run-time type informatio...
X Description: We currently only support some baked-in forms of type checking for type guards -- typeof and instanceof. In many cases, users will have their own functions that can provide run-time type informatio...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/1007
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Support user-defined type guard functions","articleBody":"We currently only support some baked-in forms of type checking for type guards -- `typeof` and `instanceof`. In many cases, users will have their own functions that can provide run-time type information.\n\nProposal: a new syntax, valid only in return type annotations, of the form `x is T` where `x` is a declared parameter in the signature, or `this`, and `T` is any type. This type is actually considered as `boolean`, but \"lights up\" type guards. Examples:\n\n``` ts\nfunction isCat(a: Animal): a is Cat {\n return a.name === 'kitty';\n}\n\nvar x: Animal;\nif(isCat(x)) {\n x.meow(); // OK, x is Cat in this block\n}\n\nclass Node {\n isLeafNode(): this is LeafNode { throw new Error('abstract'); }\n}\nclass ParentNode extends Node {\n isLeafNode(): this is LeafNode { return false; }\n}\nclass LeafNode extends Node {\n isLeafNode(): this is LeafNode { return true; }\n}\nvar someNode: LeafNode|ParentNode;\nif(someNode.isLeafNode()) {\n // someNode: LeafNode in this block\n}\n```\n\nThe forms `if(userCheck([other args,] expr [, other args])) {` and `if(expr.userCheck([any args]))` would apply the type guard to `expr` the same way that `expr instanceof t` and `typeof expr === 'literal'` do today.\n","author":{"url":"https://github.com/RyanCavanaugh","@type":"Person","name":"RyanCavanaugh"},"datePublished":"2014-10-30T21:05:34Z","url":"https://github.com/microsoft/TypeScript/issues/1007"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:897341b1-044a-e389-a504-17091b139e54 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D8DA:60624:669A:8AF8:6AFBF420 |
| html-safe-nonce | 2f2f7e76e5228409f1bc20e8bb9891e7ba73f08be703aef837e0ca91e148bcb5 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEOERBOjYwNjI0OjY2OUE6OEFGODo2QUZCRjQyMCIsInZpc2l0b3JfaWQiOiI0MjIwMDI4NjgxNjE4Njg4MzIiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | cf169fc54ccffe512aecc58db8f5047db98c62fb86d04f089eea5f6d6301787d |
| hovercard-subject-tag | issue:47327525 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/microsoft/TypeScript/1007/issue_layout |
| twitter:image | https://opengraph.githubassets.com/dc3cac701b87337d8870c48cda1b4c3d2e13d42d4549e9ec425aa396a7851f7c/microsoft/TypeScript/issues/1007 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/dc3cac701b87337d8870c48cda1b4c3d2e13d42d4549e9ec425aa396a7851f7c/microsoft/TypeScript/issues/1007 |
| og:image:alt | We currently only support some baked-in forms of type checking for type guards -- typeof and instanceof. In many cases, users will have their own functions that can provide run-time type informatio... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | RyanCavanaugh |
| hostname | github.com |
| expected-hostname | github.com |
| None | fa74413a1d84dac536fcca0740780bcbd3207bafec0e51873b73ff8e8cca032f |
| turbo-cache-control | no-preview |
| go-import | github.com/microsoft/TypeScript git https://github.com/microsoft/TypeScript.git |
| octolytics-dimension-user_id | 6154722 |
| octolytics-dimension-user_login | microsoft |
| octolytics-dimension-repository_id | 20929025 |
| octolytics-dimension-repository_nwo | microsoft/TypeScript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 20929025 |
| octolytics-dimension-repository_network_root_nwo | microsoft/TypeScript |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | b3b5633a74535d9c23385512d534c66c8fde1907 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width