Title: Provide a disciminated union of all available Node types · Issue #13634 · microsoft/TypeScript · GitHub
Open Graph Title: Provide a disciminated union of all available Node types · Issue #13634 · microsoft/TypeScript
X Title: Provide a disciminated union of all available Node types · Issue #13634 · microsoft/TypeScript
Description: TypeScript Version: 2.1.5 Recently in tslint development there was the idea to create a discriminated union of all ts.Nodes available in the AST. That would make it easier to switch on Node.kind and avoid the need of type assertions. It ...
Open Graph Description: TypeScript Version: 2.1.5 Recently in tslint development there was the idea to create a discriminated union of all ts.Nodes available in the AST. That would make it easier to switch on Node.kind an...
X Description: TypeScript Version: 2.1.5 Recently in tslint development there was the idea to create a discriminated union of all ts.Nodes available in the AST. That would make it easier to switch on Node.kind an...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/13634
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Provide a disciminated union of all available Node types","articleBody":"\u003c!-- BUGS: Please use this template. -->\r\n\u003c!-- QUESTIONS: This is not a general support forum! Ask Qs at http://stackoverflow.com/questions/tagged/typescript -->\r\n\u003c!-- SUGGESTIONS: See https://github.com/Microsoft/TypeScript-wiki/blob/master/Writing-Good-Design-Proposals.md -->\r\n\r\n**TypeScript Version:** 2.1.5\r\n\r\nRecently in `tslint` development there was the idea to create a discriminated union of all `ts.Node`s available in the AST. That would make it easier to switch on `Node.kind` and avoid the need of type assertions. It would also simplify some of `TypeScript`'s own code like `forEachChild` in parser.ts.\r\n\r\nBefore we start to create and maintain such a union ourselves, the question is if typescript can provide anything for this use case? Given there are already unions for things like `BlockLike`, `CallLikeExpression`, etc., would it be possible to have more of them, maybe even auto generated?\r\n\r\nUnfortunately that would ultimately result in the demand for more unions for `Expression`, `Statement`, ...\r\n\r\n**Code**\r\n\r\n```ts\r\nts.forEachChild(someNode, visitNode);\r\nfunction visitNode(node: ts.Node) {\r\n switch (node.kind) {\r\n case ts.SyntaxKind.Identifier:\r\n doStuff((node as ts.Identifier).text);\r\n case ts.SyntaxKind.PropertyAssignment:\r\n if ((node as ts.PropertyAssignment).initializer.kind === ts.SyntaxKind.Identifier)\r\n doStuff(((node as ts.PropertyAssignment).initializer as ts.Identifier).text);\r\n // ... to be continued\r\n }\r\n}\r\n```\r\n\r\n**Expected behavior:**\r\n\r\n```ts\r\nts.forEachChild(someNode, visitNode);\r\nfunction visitNode(node: ts.AllNodes) { // discriminated union of all subtypes of ts.Node\r\n switch (node.kind) {\r\n case ts.SyntaxKind.Identifier:\r\n doStuff(node.text); // type can be inferred\r\n case ts.SyntaxKind.PropertyAssignment:\r\n if (node.initializer.kind === ts.SyntaxKind.Identifier) // node is inferred as ts.PropertyAssignment\r\n doStuff(node.initializer.text); // node.initializer could be inferred to be ts.Identifier, would require another union for all subtypes of ts.Expression\r\n }\r\n}\r\n```\r\n\r\n//cc @nchen63\r\n","author":{"url":"https://github.com/ajafff","@type":"Person","name":"ajafff"},"datePublished":"2017-01-23T13:14:28Z","url":"https://github.com/microsoft/TypeScript/issues/13634"}
| 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:21b19b32-bc80-2a5f-d409-26ebc06ebc62 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8A8E:842EC:F40F06:146B1DD:6B010E8F |
| html-safe-nonce | 1f88fb155ababf8e107bbde1cba444d25acc7947314e740ada517e336d3da061 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4QThFOjg0MkVDOkY0MEYwNjoxNDZCMUREOjZCMDEwRThGIiwidmlzaXRvcl9pZCI6IjY0Mjg1MDIzMjU1MTQxMTM0MyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | de7b31d99c1fac5b63c404ac535bca885101ed9094c962a5822b44df77b6904c |
| hovercard-subject-tag | issue:202530478 |
| 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/13634/issue_layout |
| twitter:image | https://opengraph.githubassets.com/98d399a2de38dc054d1d4474d127c306f5053119df2d5005914d70730b41f28c/microsoft/TypeScript/issues/13634 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/98d399a2de38dc054d1d4474d127c306f5053119df2d5005914d70730b41f28c/microsoft/TypeScript/issues/13634 |
| og:image:alt | TypeScript Version: 2.1.5 Recently in tslint development there was the idea to create a discriminated union of all ts.Nodes available in the AST. That would make it easier to switch on Node.kind an... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | ajafff |
| hostname | github.com |
| expected-hostname | github.com |
| None | cb7fa109eca7d4156c889f474306b0e91336055e9adf8fd4684b61e49094d124 |
| 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 | 2b59d31a4e44f08b7af67a4b67f8fd0af3befe2a |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width