Title: Pick non-null member of nullable mapped type · Issue #15643 · microsoft/TypeScript · GitHub
Open Graph Title: Pick non-null member of nullable mapped type · Issue #15643 · microsoft/TypeScript
X Title: Pick non-null member of nullable mapped type · Issue #15643 · microsoft/TypeScript
Description: I have a simple code which checks property for null and throws exception if it is null or returns the value. function getNonNull
Open Graph Description: I have a simple code which checks property for null and throws exception if it is null or returns the value. function getNonNull
X Description: I have a simple code which checks property for null and throws exception if it is null or returns the value. function getNonNull<T extends object, K extends keyof T>(obj: T, key: K): T[K] { c...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/15643
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Pick non-null member of nullable mapped type","articleBody":"I have a simple code which checks property for null and throws exception if it is null or returns the value.\r\n\r\n```ts\r\nfunction getNonNull\u003cT extends object, K extends keyof T>(obj: T, key: K): T[K] {\r\n const v = obj[key]; \r\n if (v != null) {\r\n return v;\r\n } else {\r\n throw \"A\";\r\n }\r\n} \r\n```\r\n\r\nThe only catch that it doesn't say it returns non-null value.\r\n```ts\r\nconst a = getNonNull({a:null},\"a\"); // a : null\r\n```\r\n\r\nI am trying to improve function to set 'obj' as an object of type [K in string]: U | null | undefined and then TypeScript would return 'U'.\r\n\r\nUnfortunately TypeScript doesn't like it and types 'U' as '{}' instead of correct type.\r\n\r\n```ts\r\n\r\nfunction getNonNull\u003c\r\n U,\r\n T extends {[K in string]: U | null | undefined },\r\n K extends keyof T\r\n >(obj: T, key: K): T[K] {\r\n \r\n const v = obj[key]; \r\n if (v != null) {\r\n// error TS2322: Type 'T[K]' is not assignable to type 'U'.\r\n// Type 'U | null | undefined' is not assignable to type 'U'.\r\n// Type 'undefined' is not assignable to type 'U'.\r\n return v; \r\n } else {\r\n throw \"A\";\r\n }\r\n} \r\n```\r\n\r\nDo I miss something ?","author":{"url":"https://github.com/NN---","@type":"Person","name":"NN---"},"datePublished":"2017-05-07T19:40:59Z","url":"https://github.com/microsoft/TypeScript/issues/15643"}
| 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:a10a415d-32b4-6c34-dd78-142bce881a56 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 966E:3A73AA:5C45F5:749C3D:6B0111D9 |
| html-safe-nonce | 4350eb7f93a4567f784681878aeadac206299c6f01ccdbda68aeed628e6fcde1 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5NjZFOjNBNzNBQTo1QzQ1RjU6NzQ5QzNEOjZCMDExMUQ5IiwidmlzaXRvcl9pZCI6IjEwOTc2MTgwMTI4NTAxNjQxODUiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | cc82a0e8f9e3634e2dce8a2066d82f84e92670ea9bc52891981d3d5200722fd1 |
| hovercard-subject-tag | issue:226887678 |
| 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/15643/issue_layout |
| twitter:image | https://opengraph.githubassets.com/e413c6319db4e6a983fc3b1027f8f1ab77fcd37be4908d3fddc0e7b37fd2a2a8/microsoft/TypeScript/issues/15643 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/e413c6319db4e6a983fc3b1027f8f1ab77fcd37be4908d3fddc0e7b37fd2a2a8/microsoft/TypeScript/issues/15643 |
| og:image:alt | I have a simple code which checks property for null and throws exception if it is null or returns the value. function getNonNull |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | NN--- |
| 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