Title: Issues working with Generic Constraints · Issue #34567 · microsoft/TypeScript · GitHub
Open Graph Title: Issues working with Generic Constraints · Issue #34567 · microsoft/TypeScript
X Title: Issues working with Generic Constraints · Issue #34567 · microsoft/TypeScript
Description: TypeScript Version: 3.6.4 Search Terms: generic, generics, constraints Context Hello Typescript team! I've been working on updating my team's typescript codebase from v 3.2.1 to v 3.6.4 I'm having some issues updating my codebase to supp...
Open Graph Description: TypeScript Version: 3.6.4 Search Terms: generic, generics, constraints Context Hello Typescript team! I've been working on updating my team's typescript codebase from v 3.2.1 to v 3.6.4 I'm having ...
X Description: TypeScript Version: 3.6.4 Search Terms: generic, generics, constraints Context Hello Typescript team! I've been working on updating my team's typescript codebase from v 3.2.1 to v 3.6.4 I...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/34567
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Issues working with Generic Constraints","articleBody":"**TypeScript Version:** 3.6.4\r\n\r\n**Search Terms:**\r\ngeneric, generics, constraints\r\n\r\n**Context**\r\nHello Typescript team!\r\n\r\nI've been working on updating my team's typescript codebase from v 3.2.1 to v 3.6.4\r\n\r\nI'm having some issues updating my codebase to support the breaking changes, and wanted to start a discussion to see if what I'm coming across is by design, and if so, what is the recommended solution for working through it.\r\n\r\nBelow I'll create a simplified example of the issue we're having, and I've verified that the issue only appears after and including Typescript 3.5. I believe the issue may be related to [this](https://github.com/Microsoft/TypeScript/pull/30637)\r\n\r\n**Code**\r\n\r\n```ts\r\ninterface StoreParams {\r\n key1: string;\r\n key2: number;\r\n key3: string[];\r\n}\r\n\r\nfunction get\u003cKey extends keyof StoreParams>(key: Key): StoreParams[Key] | null {\r\n const key1Return: string = 'key1Return';\r\n const key2Return: number = 2;\r\n const key3Return: string[] = ['a', 'b', 'c'];\r\n\r\n if (key === 'key1') {\r\n return key1Return;\r\n }\r\n\r\n if (key === 'key2') {\r\n return key2Return;\r\n }\r\n\r\n if (key === 'key3') {\r\n return key3Return;\r\n }\r\n\r\n return null;\r\n}\r\n```\r\n\r\n**Expected behavior:**\r\nNo compiler errors\r\n\r\n**Actual behavior:**\r\nThe above code results in an error in each of the key return statements, respectively they are:\r\n\r\n```\r\nType 'string' is not assignable to type 'StoreParams[Key] | null'.\r\n```\r\n\r\n```\r\nType 'number' is not assignable to type 'StoreParams[Key] | null'.\r\n```\r\n\r\n```\r\nType 'string[]' is not assignable to type 'StoreParams[Key]'.\r\n Type 'string[]' is not assignable to type 'never'.\r\n```\r\n\r\nIt seems in each of the return statements, the return type is still `StoreParams[Key] | null`, where `Key` has not been filtered down after the `if` statement which normally acts as a type guard for `key`.\r\n\r\nIf I replace the generic `Key` with just `keyof StoreParams`, the type guards seem to work on `key`, but I've lost the benefit of the generic in that I'm still just returning `StoreParams[keyof StoreParams]`, meaning in each of the return statements I can return `string | number | string[] | null`.\r\n\r\n**Playground Link:** [Link here](http://www.typescriptlang.org/play/?ssl=16&ssc=10&pln=1&pc=1#code/JYOwLgpgTgZghgYwgAgMpgPZQgBTlOAWwGdkBvAKGWQGsIBPARgC5liwpQBzAbitoYAmViACuhAEbQ+1OvQDMrdpxBcA2gF0+AXwoUYokAjDAMIZFwhgAPAGkGyCAA9IIACak5GGGkzY8BCQAfAAUcqz29ACUrOhYuPhExGqRGsgAPshiADbZ5PwIZuwCTABKVqJQIEoc3MgAvMgA5HKM5WCVIE0yyIUgxXKC7Z0i4lJQDciCPX0DDPLDVTUq6mmNak1wTQA0zRI7zQhNWnrUwD5hDvXXza1NUfnU1NgdVSVtFVU9uvznyJf0Bo3FpCe6PJ4vTolIafEDfU7IP4AoGNEEKMGUJ7ISFvOQLWHw-g48w5bI6IA)\r\n\r\n**Related Issues:**\r\n","author":{"url":"https://github.com/Dsan10s","@type":"Person","name":"Dsan10s"},"datePublished":"2019-10-18T17:22:26Z","url":"https://github.com/microsoft/TypeScript/issues/34567"}
| 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:61addae4-04de-9d0f-8cf3-bf36af85879d |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B7AE:6FAD8:9D09AE:CC0CCD:6B00E18D |
| html-safe-nonce | 12ed90f66840556f234247455debe92d669d0ab47fa9aabcfb3f7044dbe93480 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCN0FFOjZGQUQ4OjlEMDlBRTpDQzBDQ0Q6NkIwMEUxOEQiLCJ2aXNpdG9yX2lkIjoiMzk4NjQwNDEyNTU1NzQ0OTEwMSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 05beac1103a06e93b4783420b0e1f96600179d6f062b8a9695ce4c06fdf45ae0 |
| hovercard-subject-tag | issue:509214728 |
| 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/34567/issue_layout |
| twitter:image | https://opengraph.githubassets.com/c30a0a1deb340bfe2649f9014776d5634d1092130f162688f20d61866a8a80de/microsoft/TypeScript/issues/34567 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/c30a0a1deb340bfe2649f9014776d5634d1092130f162688f20d61866a8a80de/microsoft/TypeScript/issues/34567 |
| og:image:alt | TypeScript Version: 3.6.4 Search Terms: generic, generics, constraints Context Hello Typescript team! I've been working on updating my team's typescript codebase from v 3.2.1 to v 3.6.4 I'm having ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Dsan10s |
| hostname | github.com |
| expected-hostname | github.com |
| None | 043c3f30fbcbe2c6c8e729e10cbbbc90c406546f9fbbb67c648d38f6b7d16daf |
| 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 | 5425dc24e8bd8d6fca9641a12afc984825a43875 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width