Title: Propagate Never Types to Parents · Issue #26537 · microsoft/TypeScript · GitHub
Open Graph Title: Propagate Never Types to Parents · Issue #26537 · microsoft/TypeScript
X Title: Propagate Never Types to Parents · Issue #26537 · microsoft/TypeScript
Description: Search Terms never type inference, never parent, never property, never narrowing Suggestion TypeScript's type system helpfully eliminates never patterns from unions. This could be even more helpful if it were able to infer that types con...
Open Graph Description: Search Terms never type inference, never parent, never property, never narrowing Suggestion TypeScript's type system helpfully eliminates never patterns from unions. This could be even more helpful...
X Description: Search Terms never type inference, never parent, never property, never narrowing Suggestion TypeScript's type system helpfully eliminates never patterns from unions. This could be even more hel...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/26537
X: @github
Domain: github.com
IPv4 address: 140.82.112.4
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Propagate Never Types to Parents","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[never type inference](https://github.com/Microsoft/TypeScript/issues?utf8=%E2%9C%93&q=is%3Aissue+never+type+inference), [never parent](https://github.com/Microsoft/TypeScript/issues?utf8=%E2%9C%93&q=is%3Aissue+never+parent), [never property](https://github.com/Microsoft/TypeScript/issues?utf8=%E2%9C%93&q=is%3Aissue+never+property), [never narrowing](https://github.com/Microsoft/TypeScript/issues?utf8=%E2%9C%93&q=is%3Aissue+never+narrowing+)\r\n\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\nTypeScript's type system helpfully eliminates `never` patterns from unions. This could be even more helpful if it were able to infer that types containing `never` types can not exist at runtime, and must themselves also be `never`. \r\n\r\nAny types which contain a not-null, not-undefined, not-unioned value of the `never` type would themselves be reduced to a `never` type, since they can also never exist.\r\n\r\nThis applies when `never` is used as a property value, array type, or tuple value...\r\n\r\n- `{x: never, y: number}` becomes `never`\r\n- `never[]` becomes `never`\r\n- `(never, 1)` becomes `never`\r\n\r\nbut not when `never` is used as a return value, or part of a union...\r\n\r\n- `() => never` is unchanged \r\n- `(never) => void` is unchanged\r\n- `never|String` already becomes `String`\r\n\r\nWould this be a reasonable transformation to perform?\r\n\r\nThis is inspired by [part of this never type handling proposal for Rust](http://smallcultfollowing.com/babysteps/blog/2018/08/13/never-patterns-exhaustive-matching-and-uninhabited-types-oh-my/#implications-for-safe-code).\r\n\r\n## Example\r\n\r\nConsider using a generic `Result\u003cT, U>` type, which contains a value of type `T` in case of success or one of type `U` in case of failure. If we need to return a `Result`, but know that no error can occur for our implementation, we might use the type `Result\u003cString, never>`.\r\n\r\nIf we use an untagged union, TypeScript recognizes that the error case can never occur and knows we can safely treat it as the success type.\r\n\r\n```typescript\r\ntype Result\u003cT, U> = T | U;\r\n\r\nconst s1 = \"hello\" as Result\u003cString, Error>;\r\ns1.slice();\r\n// Property 'slice' does not exist on type 'Result\u003cString, Error>'.\r\n// Property 'slice' does not exist on type 'Error'.\r\n\r\nconst s2 = \"hello\" as Result\u003cString, never>;\r\ns2.slice();\r\n// \"hello\"\r\n```\r\n\r\nHowever, if we used a tagged union instead, TypeScript can't eliminate the failure type:\r\n\r\n```typescript\r\ntype Result\u003cT, U> = {ok: T} | {err: U};\r\n\r\nconst s1 = {ok: \"hello\"} as Result\u003cString, Error>;\r\ns1.ok.slice();\r\n// Property 'ok' does not exist on type 'Result\u003cString, Error>'. \r\n// Property 'ok' does not exist on type '{ err: Error; }'.\r\n\r\nconst s2 = {ok: \"hello\"} as Result\u003cString, never>;\r\ns2.ok.slice();\r\n// Property 'ok' does not exist on type 'Result\u003cString, never>'. \r\n// Property 'ok' does not exist on type '{ err: never; }'.\r\n```\r\n\r\nIf the `never` type were propagated up in `{ err: never }`, the compiler could eliminate it from the union and recognize that this code was safe.\r\n\r\n## Compatibility\r\n\r\nCode that operates on structures containing never values could start raising new errors. For example, the following currently [compiles without error](https://www.typescriptlang.org/play/index.html#src=declare%20const%20never%3A%20never%3B%0D%0A%0D%0Aconst%20p%20%3D%20%7Bx%3A%20never%2C%20y%3A%200%7D%0D%0Aconst%20q%20%3D%20p.y%3B%0D%0Aconsole.log(p)%3B%0D%0A), but after this change it would raise an error:\r\n\r\n```rust\r\ndeclare const never: never;\r\nconst p = {x: never, y: 0}\r\nconst q = p.y;\r\n// Property 'y' does not exist on type '{x: never, y: 0}'.\r\n// Property 'y' does not exist on type 'never'.\r\n```\r\n\r\nSuch code is necessarily already dead, so this case shouldn't be very common.\r\n\r\nThe change wouldn't introduce errors in generic code that just might be populated with `never`, like the motivating example above.\r\n\r\n\r\n## Checklist\r\n\r\nMy suggestion meets these guidelines:\r\n* [ ] This wouldn't be a breaking change in existing TypeScript / JavaScript code\r\n - This **is** a breaking change for some existing TypeScript code, but it might only affect dead 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","author":{"url":"https://github.com/jeremyBanks","@type":"Person","name":"jeremyBanks"},"datePublished":"2018-08-18T17:23:45Z","url":"https://github.com/microsoft/TypeScript/issues/26537"}
| 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:adcc7b97-fdf6-ef78-0732-2e8348b5a7f1 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E502:3DD456:412CC:58443:6B04A81C |
| html-safe-nonce | 208a5ec6be0f620db65f4c5c3af87dc63e6614890e39d49dd9e44457803f6971 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFNTAyOjNERDQ1Njo0MTJDQzo1ODQ0Mzo2QjA0QTgxQyIsInZpc2l0b3JfaWQiOiI3NDY1ODYwNjM2NDY0MTYyODQ0IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | f9449e4460e17be9b3f27650032600a4d28b0b15fd306ec651f8afedeb35e1b1 |
| hovercard-subject-tag | issue:351837210 |
| 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/26537/issue_layout |
| twitter:image | https://opengraph.githubassets.com/2a030bf5cc85f2d65d52a2eba4e4f7872da0e6ef2a97358dffc4d519c9cae6b1/microsoft/TypeScript/issues/26537 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/2a030bf5cc85f2d65d52a2eba4e4f7872da0e6ef2a97358dffc4d519c9cae6b1/microsoft/TypeScript/issues/26537 |
| og:image:alt | Search Terms never type inference, never parent, never property, never narrowing Suggestion TypeScript's type system helpfully eliminates never patterns from unions. This could be even more helpful... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | jeremyBanks |
| hostname | github.com |
| expected-hostname | github.com |
| None | 188497a0f45a7aa8f7c866faa1950d1a7f8d82b59416adf549b222ad1d1dd19b |
| 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 | 557cfbac6cdb532730c95c90f2cefdc0c4128029 |
| 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