Title: Change in generic behavior between 3.0.1 and >=3.1.1 · Issue #27895 · microsoft/TypeScript · GitHub
Open Graph Title: Change in generic behavior between 3.0.1 and >=3.1.1 · Issue #27895 · microsoft/TypeScript
X Title: Change in generic behavior between 3.0.1 and >=3.1.1 · Issue #27895 · microsoft/TypeScript
Description: TypeScript Version: 3.2.0-dev.20181011 This code worked in 3.0.1, and is broken >=3.1.1. Search Terms: "is not assignable to type" Code export interface Entity { id: number | string; } export type IdOf
Open Graph Description: TypeScript Version: 3.2.0-dev.20181011 This code worked in 3.0.1, and is broken >=3.1.1. Search Terms: "is not assignable to type" Code export interface Entity { id: number | string; } export type ...
X Description: TypeScript Version: 3.2.0-dev.20181011 This code worked in 3.0.1, and is broken >=3.1.1. Search Terms: "is not assignable to type" Code export interface Entity { id: number | string; }...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/27895
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Change in generic behavior between 3.0.1 and >=3.1.1","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. Even if you think you've found a *bug*, please read the FAQ first, especially the Common \"Bugs\" That Aren't Bugs section!\r\n\r\nPlease help us by doing the following steps before logging an issue:\r\n * Search: https://github.com/Microsoft/TypeScript/search?type=Issues\r\n * Read the FAQ: https://github.com/Microsoft/TypeScript/wiki/FAQ\r\n\r\nPlease fill in the *entire* template below.\r\n-->\r\n\r\n\u003c!-- Please try to reproduce the issue with `typescript@next`. It may have already been fixed. -->\r\n**TypeScript Version:** 3.2.0-dev.20181011\r\nThis code worked in 3.0.1, and is broken >=3.1.1.\r\n\r\n\u003c!-- Search terms you tried before logging this (so others can find this issue more easily) -->\r\n**Search Terms:**\r\n\"is not assignable to type\"\r\n\r\n**Code**\r\n\r\n```ts\r\nexport interface Entity {\r\n id: number | string;\r\n}\r\n\r\nexport type IdOf\u003cE extends Entity> = E['id'];\r\n\r\nexport interface EntityState\u003cE extends Entity> {\r\n ids: IdOf\u003cE>[];\r\n entities: { [key: string]: E, [key: number]: E };\r\n}\r\n\r\n\r\nexport function getAllEntities\u003cE extends Entity>(state: EntityState\u003cE>): E[] {\r\n const { ids, entities } = state;\r\n return ids.map(id => entities[id]);\r\n}\r\n\r\nexport function getEntity\u003cE extends Entity>(id: IdOf\u003cE>, state: EntityState\u003cE>): E | undefined {\r\n const { ids, entities } = state;\r\n\r\n if (!ids.includes(id)) {\r\n return undefined;\r\n }\r\n\r\n return entities[id];\r\n}\r\n```\r\n\r\nFor completeness, I'll just mention that I intend for users to create subinterfaces of `Entity` and `EntityState` for their domain types (like `@ngrx/entity`), overriding `id` to be a more restrictive type. This is why the `IdOf` type is important. E.g.,\r\n\r\n```ts\r\ninterface Task extends Entity {\r\n id: string;\r\n due: Date;\r\n title: string;\r\n}\r\n\r\ninterface TaskState extends EntityState\u003cTask> {\r\n // Inherits EntityState#ids as string[]\r\n currentTask?: string;\r\n}\r\n```\r\n\r\n**Expected behavior:**\r\nThe code compiles without error.\r\n\r\n**Actual behavior:**\r\n```\r\nBUILD ERROR\r\nprojects/entity/src/lib/entity-state.utils.ts(13,5): error TS2322: Type '{ [key: string]: E; [key: number]: E; }[E[\"id\"]][]' is not assignable to type 'E[]'.\r\n Type '{ [key: string]: E; [key: number]: E; }[E[\"id\"]]' is not assignable to type 'E'.\r\n Type 'Entity' is not assignable to type 'E'.\r\nprojects/entity/src/lib/entity-state.utils.ts(23,5): error TS2322: Type '{ [key: string]: E; [key: number]: E; }[E[\"id\"]]' is not assignable to type 'E | undefined'.\r\n Type 'Entity' is not assignable to type 'E'.\r\n Type '{ [key: string]: E; [key: number]: E; }[E[\"id\"]]' is not assignable to type 'E'.\r\n Type 'Entity' is not assignable to type 'E'.\r\n```\r\n\r\n**Playground Link:** \u003c!-- A link to a TypeScript Playground \"Share\" link which demonstrates this behavior -->\r\n[Playground Link](http://www.typescriptlang.org/play/#src=export%20interface%20Entity%20%7B%0D%0A%20%20%20%20id%3A%20number%20%7C%20string%3B%0D%0A%7D%0D%0A%0D%0Aexport%20type%20IdOf%3CE%20extends%20Entity%3E%20%3D%20E%5B'id'%5D%3B%0D%0A%0D%0Aexport%20interface%20EntityState%3CE%20extends%20Entity%3E%20%7B%0D%0A%20%20%20%20ids%3A%20IdOf%3CE%3E%5B%5D%3B%0D%0A%20%20%20%20entities%3A%20%7B%20%5Bkey%3A%20string%5D%3A%20E%2C%20%5Bkey%3A%20number%5D%3A%20E%20%7D%3B%0D%0A%7D%0D%0A%0D%0A%0D%0Aexport%20function%20getAllEntities%3CE%20extends%20Entity%3E(state%3A%20EntityState%3CE%3E)%3A%20E%5B%5D%20%7B%0D%0A%20%20%20%20const%20%7B%20ids%2C%20entities%20%7D%20%3D%20state%3B%0D%0A%20%20%20%20return%20ids.map(id%20%3D%3E%20entities%5Bid%5D)%3B%0D%0A%7D%0D%0A%0D%0Aexport%20function%20getEntity%3CE%20extends%20Entity%3E(id%3A%20IdOf%3CE%3E%2C%20state%3A%20EntityState%3CE%3E)%3A%20E%20%7C%20undefined%20%7B%0D%0A%20%20%20%20const%20%7B%20ids%2C%20entities%20%7D%20%3D%20state%3B%0D%0A%0D%0A%20%20%20%20if%20(!ids.includes(id))%20%7B%0D%0A%20%20%20%20%20%20%20%20return%20undefined%3B%0D%0A%20%20%20%20%7D%0D%0A%0D%0A%20%20%20%20return%20entities%5Bid%5D%3B%0D%0A%7D)\r\n\r\n**Related Issues:** \u003c!-- Did you find other bugs that looked similar? -->\r\nI did not find any that seemed similar, but I'm also unsure what the actual underlying problem is. I did [ask on StackOverflow](https://stackoverflow.com/q/52798934/4450643), and they suggested I ask here.","author":{"url":"https://github.com/chrisbouchard","@type":"Person","name":"chrisbouchard"},"datePublished":"2018-10-14T13:29:49Z","url":"https://github.com/microsoft/TypeScript/issues/27895"}
| 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:175443df-06b4-664b-7680-886c51a78794 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 86C0:2A5566:DEBF13:124AA1A:6B01CB58 |
| html-safe-nonce | 3a9887d73c36b47ff7124d8e0f8e3716ef0f04749f25a3e8c79c2d6de60e6206 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4NkMwOjJBNTU2NjpERUJGMTM6MTI0QUExQTo2QjAxQ0I1OCIsInZpc2l0b3JfaWQiOiI4NzE2MDY3MzM3OTE4MzM1ODMyIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 893a0b1d8c36aa68d93c4b2fce6f6274a70bbd234d77ad3bff603050d422e5a7 |
| hovercard-subject-tag | issue:369902466 |
| 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/27895/issue_layout |
| twitter:image | https://opengraph.githubassets.com/a3932989caa2ede65880daa8497e5e231f1efb7927b328c140248e9d16bd7893/microsoft/TypeScript/issues/27895 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/a3932989caa2ede65880daa8497e5e231f1efb7927b328c140248e9d16bd7893/microsoft/TypeScript/issues/27895 |
| og:image:alt | TypeScript Version: 3.2.0-dev.20181011 This code worked in 3.0.1, and is broken >=3.1.1. Search Terms: "is not assignable to type" Code export interface Entity { id: number | string; } export type ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | chrisbouchard |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| 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 | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width