Title: Non-nullish assertion over property of generic object not respected · Issue #44698 · microsoft/TypeScript · GitHub
Open Graph Title: Non-nullish assertion over property of generic object not respected · Issue #44698 · microsoft/TypeScript
X Title: Non-nullish assertion over property of generic object not respected · Issue #44698 · microsoft/TypeScript
Description: Bug Report I'm building a function that removes nullish-values from an object. In the code, that is generic over the object, TypeScript doesn't seem to respect my non-nullish assertion: if (input[key] != null) result[key] = input[key] It...
Open Graph Description: Bug Report I'm building a function that removes nullish-values from an object. In the code, that is generic over the object, TypeScript doesn't seem to respect my non-nullish assertion: if (input[k...
X Description: Bug Report I'm building a function that removes nullish-values from an object. In the code, that is generic over the object, TypeScript doesn't seem to respect my non-nullish assertion: if ...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/44698
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Non-nullish assertion over property of generic object not respected","articleBody":"# Bug Report\r\n\r\nI'm building a function that removes nullish-values from an object. In the code, that is generic over the object, TypeScript doesn't seem to respect my non-nullish assertion:\r\n\r\n```ts\r\nif (input[key] != null) result[key] = input[key]\r\n```\r\n\r\nIt seems to think that `input[key]` could possibly be null in the assignment, even though the line is guarded by `input[key] != null`.\r\n\r\n### 🔎 Search Terms\r\n\r\nnullish assertion generics assignment\r\n\r\n### 🕗 Version & Regression Information\r\n\r\n- This is the behavior in every version I tried, and I reviewed the FAQ for entries about: v4.4.0-dev.20210622, v4.3.4, v4.3.2, v3.3.33\r\n\r\n### ⏯ Playground Link\r\n\r\n[Playground link with relevant code](https://www.typescriptlang.org/play?#code/C4TwDgpgBACghgJ2ASzgGwHIHsB2GCuaacARmhADwAqAfFALxQDeUA2jFMjlANYQhYAZlCoBdAPwAuKNjyFiZSlXai6AXwBQGwfhwBjFLigB3ZMAAWWfMAJFkAZ3MA1dPgj3qUCAA9gEHAAm9lBYJABWEAZ0ABRcYNbSVACU0vBIqJi4tgrk1HRMGlBQerj2wFAI7oTAqYgo6LLZpLm0DMyahVCCWAhQ0SU4Zbz8IcIA8uGRwAB0fCD2sTjxwElQcMHRc0IiSayiqwVFRcjCi8usc6JQAISMOPKrlfbVF-xXjHHWryCinR1FlWA+AQ3Ce1Q0aiAA)\r\n\r\n### 💻 Code\r\n\r\n```ts\r\ntype PartialNonNullable\u003cT> = { [P in keyof T]?: NonNullable\u003cT[P]> }\r\n\r\nfunction withoutNullishValues\u003cT extends object> (input: T): PartialNonNullable\u003cT> {\r\n const result: PartialNonNullable\u003cT> = {}\r\n\r\n for (const key of Object.keys(input) as (keyof T)[]) {\r\n if (input[key] != null) result[key] = input[key]\r\n }\r\n\r\n return result\r\n}\r\n```\r\n\r\n### 🙁 Actual behavior\r\n\r\n```text\r\nType 'T[keyof T]' is not assignable to type 'NonNullable\u003cT[keyof T]> | undefined'.\r\n Type 'T[string] | T[number] | T[symbol]' is not assignable to type 'NonNullable\u003cT[keyof T]> | undefined'.\r\n Type 'T[string]' is not assignable to type 'NonNullable\u003cT[keyof T]> | undefined'.\r\n Type 'T[string]' is not assignable to type 'NonNullable\u003cT[keyof T]>'.\r\n Type 'T[keyof T]' is not assignable to type 'NonNullable\u003cT[keyof T]>'.\r\n Type 'T[string] | T[number] | T[symbol]' is not assignable to type 'NonNullable\u003cT[keyof T]>'.\r\n Type 'T[string]' is not assignable to type 'NonNullable\u003cT[keyof T]>'.\r\n```\r\n\r\n### 🙂 Expected behavior\r\n\r\nno errors in type checking\r\n","author":{"url":"https://github.com/LinusU","@type":"Person","name":"LinusU"},"datePublished":"2021-06-22T16:49:08Z","url":"https://github.com/microsoft/TypeScript/issues/44698"}
| 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:3dbb1d8e-13e6-442a-7434-ee4b60b648df |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8914:17FB7:22822:2B315:6B00F34E |
| html-safe-nonce | 65a17b4c29af4a506ec6cac850b6f3eab5cb2d50e932a60b0bf7a42d3b7ea8f8 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4OTE0OjE3RkI3OjIyODIyOjJCMzE1OjZCMDBGMzRFIiwidmlzaXRvcl9pZCI6IjE3MDM2MDc1NjI5NjE3NDQ3MTgiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | b9aa254a8581f982275730a54f4ccb20a8361d7f9c47f8531e7fdf17a09aecd2 |
| hovercard-subject-tag | issue:927443050 |
| 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/44698/issue_layout |
| twitter:image | https://opengraph.githubassets.com/292043957f63a95bb1e2d9e8b78ef8e35ddb2da77f7df36e59944c2b8ebb238b/microsoft/TypeScript/issues/44698 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/292043957f63a95bb1e2d9e8b78ef8e35ddb2da77f7df36e59944c2b8ebb238b/microsoft/TypeScript/issues/44698 |
| og:image:alt | Bug Report I'm building a function that removes nullish-values from an object. In the code, that is generic over the object, TypeScript doesn't seem to respect my non-nullish assertion: if (input[k... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | LinusU |
| 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