Title: Allow to use null as discriminator for tagged union · Issue #24193 · microsoft/TypeScript · GitHub
Open Graph Title: Allow to use null as discriminator for tagged union · Issue #24193 · microsoft/TypeScript
X Title: Allow to use null as discriminator for tagged union · Issue #24193 · microsoft/TypeScript
Description: Search Terms Suggestion Right now, even with strictNullChecks is set to true, null cannot be used as a discriminator for tagged union. Use Cases Recently, I made a pull request for @types/node in which I let spawnSync returns a tagged un...
Open Graph Description: Search Terms Suggestion Right now, even with strictNullChecks is set to true, null cannot be used as a discriminator for tagged union. Use Cases Recently, I made a pull request for @types/node in w...
X Description: Search Terms Suggestion Right now, even with strictNullChecks is set to true, null cannot be used as a discriminator for tagged union. Use Cases Recently, I made a pull request for @types/node in w...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/24193
X: @github
Domain: github.com
IPv4 address: 140.82.112.3
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Allow to use null as discriminator for tagged union","articleBody":"\u003c!-- 🚨 STOP 🚨 𝗦𝗧𝗢𝗣 🚨 𝑺𝑻𝑶𝑷 🚨\r\n\r\nHalf of all issues filed here are duplicates, answered in the FAQ, or not appropriate for the bug tracker. Please read the FAQ first, especially the \"Common Feature Requests\" section.\r\n\r\n-->\r\n\r\n## Search Terms\r\n\r\n\u003c!-- List of keywords you searched for before creating this issue. Write them down here so that others can find this suggestion more easily -->\r\n\r\n## Suggestion\r\n\r\n\u003c!-- A summary of what you'd like to see added or changed -->\r\n\r\nRight now, even with `strictNullChecks` is set to `true`, `null` cannot be used as a discriminator for tagged union.\r\n\r\n## Use Cases\r\n\r\n\u003c!--\r\nWhat do you want to use this for?\r\nWhat shortcomings exist with current approaches?\r\n-->\r\n\r\nRecently, I made a [pull request](https://github.com/DefinitelyTyped/DefinitelyTyped/pull/25731) for `@types/node` in which I let `spawnSync` returns a tagged union with `error` property as discriminator. \r\n\r\n## Examples\r\n\r\n\u003c!-- Show how this would be used and what the behavior would be -->\r\n\r\n```typescript\r\ninterface WithError {\r\n error: Error\r\n data: null\r\n}\r\n\r\ninterface WithoutError\u003cData> {\r\n error: null\r\n data: Data\r\n}\r\n\r\ntype DataCarrier\u003cData> = WithError | WithoutError\u003cData>\r\n\r\n// MAIN FOCUS:\r\n// TypeScript should be able to deduce error's and data's\r\n// types in the following function \r\nfunction test\u003cData>(carrier: DataCarrier\u003cData>) {\r\n if (carrier.error === null) { // `error` property as disciminator\r\n const error: null = carrier.error // should work\r\n const data: Data = carrier.data // should work\r\n } else {\r\n const error: Error = carrier.error // should work\r\n const data: null = carrier.data // should work\r\n }\r\n}\r\n```\r\n\r\n## Checklist\r\n\r\nMy suggestion meets these guidelines:\r\n* [x] This wouldn't be a breaking change in existing TypeScript / JavaScript code\r\n* [x] This wouldn't change the runtime behavior of existing JavaScript code\r\n* [x] This could be implemented without emitting different JS based on the types of the expressions\r\n* [x] This isn't a runtime feature (e.g. new expression-level syntax)\r\n\r\n","author":{"url":"https://github.com/KSXGitHub","@type":"Person","name":"KSXGitHub"},"datePublished":"2018-05-17T08:50:50Z","url":"https://github.com/microsoft/TypeScript/issues/24193"}
| 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:beec265e-75b0-8e1c-31a3-16a6d9ec008b |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D36A:C24D0:59CC2F:7D362F:6B0481E1 |
| html-safe-nonce | 5f59f5de76a72514a38bd6b4faee9598d3ce2e509a041378b3ec2a93666ff7fc |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEMzZBOkMyNEQwOjU5Q0MyRjo3RDM2MkY6NkIwNDgxRTEiLCJ2aXNpdG9yX2lkIjoiMjgwMDkwNzU1NzQ1ODQ0NDc2OSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | d51170f4d6dd2d80b967d2035a13adecee878a0cbda99c1b0f2c534fcdbec542 |
| hovercard-subject-tag | issue:323928442 |
| 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/24193/issue_layout |
| twitter:image | https://opengraph.githubassets.com/86b34dbe2a1b797638d1efbd0c08617cec40cf8796f88caa1cf892900d244e29/microsoft/TypeScript/issues/24193 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/86b34dbe2a1b797638d1efbd0c08617cec40cf8796f88caa1cf892900d244e29/microsoft/TypeScript/issues/24193 |
| og:image:alt | Search Terms Suggestion Right now, even with strictNullChecks is set to true, null cannot be used as a discriminator for tagged union. Use Cases Recently, I made a pull request for @types/node in w... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | KSXGitHub |
| hostname | github.com |
| expected-hostname | github.com |
| None | 0366ddfb372682a32e92a5d86155cb08fa83e5bbe79953a968789f2f7e24c511 |
| 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 | ed3f2e01f6b7672780c586c8af0e72726374d7b6 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width