Title: Type Narrowing for discriminated unions not narrow enough · Issue #12955 · microsoft/TypeScript · GitHub
Open Graph Title: Type Narrowing for discriminated unions not narrow enough · Issue #12955 · microsoft/TypeScript
X Title: Type Narrowing for discriminated unions not narrow enough · Issue #12955 · microsoft/TypeScript
Description: TypeScript Version: 2.2.0-dev.20161215 Code type Kinded = { kind: 0 } | { kind: 1 } function forKinded ( k: Kinded ) { if ( k.kind == 0 ) console.log('ZERO') else console.log('ONE') } const k = { kind: 0 } const k2: Kinded = { kind: 0 } ...
Open Graph Description: TypeScript Version: 2.2.0-dev.20161215 Code type Kinded = { kind: 0 } | { kind: 1 } function forKinded ( k: Kinded ) { if ( k.kind == 0 ) console.log('ZERO') else console.log('ONE') } const k = { k...
X Description: TypeScript Version: 2.2.0-dev.20161215 Code type Kinded = { kind: 0 } | { kind: 1 } function forKinded ( k: Kinded ) { if ( k.kind == 0 ) console.log('ZERO') else console.log('ONE')...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/12955
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Type Narrowing for discriminated unions not narrow enough","articleBody":"**TypeScript Version:** 2.2.0-dev.20161215\r\n\r\n**Code**\r\n\r\n```ts\r\ntype Kinded\r\n = { kind: 0 }\r\n | { kind: 1 }\r\n\r\nfunction forKinded ( k: Kinded ) {\r\n if ( k.kind == 0 ) console.log('ZERO')\r\n else console.log('ONE')\r\n}\r\n\r\nconst k = { kind: 0 }\r\nconst k2: Kinded = { kind: 0 }\r\n\r\nforKinded(k) // nope. it has already guessed that k is of type { kind: number }\r\nforKinded(k2) // works. specifying type obviously does the right thing\r\nforKinded({ kind: 0 }) // works. it understands that the argument literal is of type { kind: 0 }\r\n```\r\n\r\n**Expected behavior:**\r\nWould expect it to try narrowing the type to a literal from the enumeration if the general enum doesn't satisfy other type expectations.\r\n**Actual behavior:**\r\nWhen defined as a literal function argument, typescript further narrows the type, otherwise it does not? This seems perhaps non-awesome or at least the compiler maybe ought to try further narrowing as a constraint to see if it type-checks the entirety of the program?","author":{"url":"https://github.com/stevekane","@type":"Person","name":"stevekane"},"datePublished":"2016-12-15T20:53:14Z","url":"https://github.com/microsoft/TypeScript/issues/12955"}
| 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:23d2b0f5-7b40-d0d3-975c-a74c88ec88fa |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A1D4:237673:98E8FD:CDB0A6:6B00EA3F |
| html-safe-nonce | 17d8a4a6721c49b5d7f9063fa0753927a732e9a4d127fc59ad8616d6155c18e8 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBMUQ0OjIzNzY3Mzo5OEU4RkQ6Q0RCMEE2OjZCMDBFQTNGIiwidmlzaXRvcl9pZCI6IjIwODgzMzU2MDIxNjc2Mzg1OTEiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 62bf4116b37b6e68500ea70fb839333ce6004156b3f1db20ca347e28e436d46d |
| hovercard-subject-tag | issue:195913167 |
| 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/12955/issue_layout |
| twitter:image | https://opengraph.githubassets.com/89c494edd57fe564bd238dd9286f0612a3fe8f667a8d06d2d5f698a88cd9631d/microsoft/TypeScript/issues/12955 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/89c494edd57fe564bd238dd9286f0612a3fe8f667a8d06d2d5f698a88cd9631d/microsoft/TypeScript/issues/12955 |
| og:image:alt | TypeScript Version: 2.2.0-dev.20161215 Code type Kinded = { kind: 0 } | { kind: 1 } function forKinded ( k: Kinded ) { if ( k.kind == 0 ) console.log('ZERO') else console.log('ONE') } const k = { k... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | stevekane |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4ef498cbabcbe3c50befc7a5b743e7072ee770b4daf0e15e54801180d1fa91fc |
| 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 | 9983dd5da06943f833c971dd5b6bc988e2858be5 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width