Title: Generic parameter type checking stops too early · Issue #59049 · microsoft/TypeScript · GitHub
Open Graph Title: Generic parameter type checking stops too early · Issue #59049 · microsoft/TypeScript
X Title: Generic parameter type checking stops too early · Issue #59049 · microsoft/TypeScript
Description: 🔎 Search Terms generic, early, permature, defer 🕗 Version & Regression Information This is the behavior in every version I tried, and I reviewed the FAQ for entries about _________ ⏯ Playground Link https://www.typescriptlang.org/play/?s...
Open Graph Description: 🔎 Search Terms generic, early, permature, defer 🕗 Version & Regression Information This is the behavior in every version I tried, and I reviewed the FAQ for entries about _________ ⏯ Playground Lin...
X Description: 🔎 Search Terms generic, early, permature, defer 🕗 Version & Regression Information This is the behavior in every version I tried, and I reviewed the FAQ for entries about _________ ⏯ Playground...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/59049
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Generic parameter type checking stops too early","articleBody":"### 🔎 Search Terms\r\n\r\ngeneric, early, permature, defer\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 _________\r\n\r\n\r\n### ⏯ Playground Link\r\n\r\nhttps://www.typescriptlang.org/play/?ssl=10&ssc=1&pln=1&pc=1#code/JYOwLgpgTgZghgYwgAgEoRgeQEYCsIJgA8AKgHzIDeAUMncggK5RQTgBcyJyAPsiIwA2g6gF9q1ACYFBcVshiMQhYAHsQySAGcwACgAendFjwFiAgLbZoZAJScAbquCSA3FJlyUi5WDUaAcwgwY117NAwcfEIiS2soXn4hQTJ3akFg5AdkAF5kIJCMMPdtPQdbdyA\r\n\r\n### 💻 Code\r\n\r\n```ts\r\ninterface RefObject\u003cT> {\r\n current: T | null\r\n}\r\n\r\ndeclare function test(x: RefObject\u003cnumber>): void;\r\ndeclare function getRef(): RefObject\u003cnumber | null>;\r\n\r\nlet v = getRef();\r\ntest(v);\r\n```\r\n\r\n\r\n### 🙁 Actual behavior\r\n\r\nIn the above code, `RefObject` is defined to always accept `null` in its `current` property. However, if one explicitly writes `RefObject\u003cnumber | null>` and passes the result to a parameter accepting `RefObject\u003cnumber>` TS reports a type error even though the types of the `current` property would have been the same. This happens with any type, not just null (e.g. if the definition of current was `T | string`.\r\n\r\n### 🙂 Expected behavior\r\n\r\nSince the `current` properties are equivalent after substituting the generic parameters, there should be no error. It seems like TS is stopping as soon as it sees `RefObject\u003cnumber | null>` vs `RefObject\u003cnumber>` even though substituting the generic parameter would result in the same type. This can be confirmed by creating a copy of the RefObject type like this:\r\n\r\n```ts\r\ninterface RefObject\u003cT> {\r\n current: T | null\r\n}\r\n\r\ninterface RefObject2\u003cT> {\r\n current: T | null\r\n}\r\n\r\ndeclare function test(x: RefObject\u003cnumber>): void;\r\ndeclare function getRef(): RefObject2\u003cnumber | null>;\r\n\r\nlet v = getRef();\r\ntest(v);\r\n```\r\n\r\n[Playground](https://www.typescriptlang.org/play/?ts=5.5.2&ssl=14&ssc=1&pln=1&pc=1#code/JYOwLgpgTgZghgYwgAgEoRgeQEYCsIJgA8AKgHzIDeAUMncggK5RQTgBcyJyAPsiIwA2g6gF9q1UJFiIU6LHgJgATKQo16DZqw5de-ISPHUAJgUFxWyGIxCFgAexDJIAZzAAKAB6d5OfIREAgC22NBkAJScAG4OwCYA3Kbmlig2dmCOzgDmEGDyHlFoGP5KqiFhUPoCwmRJ1IJ5yNHIALzIufkYhUluntERSUA)\r\n\r\n### Additional information about the issue\r\n\r\nThis affects the change in React's types between version 18 and 19, where the `RefObject` type changed `current` from `T | null` to only `T`. The uses of `RefObject` should add `null` instead, e.g. `RefObject\u003cHTMLDivElement | null>`. Making libraries compatible with both the React 18 and React 19 types is difficult due to this TS issue. If a library updates its implementation to React 19, including `RefObject\u003cHTMLElement | null>` in its definitions, consumers still using React 18 types will see TS errors.\r\n\r\ncc. @eps1lon in case you know about this","author":{"url":"https://github.com/devongovett","@type":"Person","name":"devongovett"},"datePublished":"2024-06-27T02:15:55Z","url":"https://github.com/microsoft/TypeScript/issues/59049"}
| 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:e81de0fc-b5c8-e765-fa4c-3adfe712bfb3 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 86F8:2454CC:2E9000F:3B1F6E4:6B01C141 |
| html-safe-nonce | 254cd6fe6d65f4b4cfb42ff26233039dcfb7f1cc368ca0b7b323f0d1ffba9839 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4NkY4OjI0NTRDQzoyRTkwMDBGOjNCMUY2RTQ6NkIwMUMxNDEiLCJ2aXNpdG9yX2lkIjoiNzU3MTUzNjY3MDM5OTUxMjg5NyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | b03ff2506cede804f0e94461ca469e2e7b4f19b24ce43025f64d6afa25b23b03 |
| hovercard-subject-tag | issue:2376565418 |
| 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/59049/issue_layout |
| twitter:image | https://opengraph.githubassets.com/7d360eb6281411c45ab4fbcf03173b34a8c80a9cc111fa5d0be16d684530e8fe/microsoft/TypeScript/issues/59049 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/7d360eb6281411c45ab4fbcf03173b34a8c80a9cc111fa5d0be16d684530e8fe/microsoft/TypeScript/issues/59049 |
| og:image:alt | 🔎 Search Terms generic, early, permature, defer 🕗 Version & Regression Information This is the behavior in every version I tried, and I reviewed the FAQ for entries about _________ ⏯ Playground Lin... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | devongovett |
| 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