Title: Generic type parameter in plain function is checked contravariantly. · Issue #16797 · microsoft/TypeScript · GitHub
Open Graph Title: Generic type parameter in plain function is checked contravariantly. · Issue #16797 · microsoft/TypeScript
X Title: Generic type parameter in plain function is checked contravariantly. · Issue #16797 · microsoft/TypeScript
Description: I don't know if it is intended. But it seems very hard for users to understand. Note in the #15104, it seems type parameter in generic type is checked co-variantly only when the type parameter is used in the parameter position of a callb...
Open Graph Description: I don't know if it is intended. But it seems very hard for users to understand. Note in the #15104, it seems type parameter in generic type is checked co-variantly only when the type parameter is u...
X Description: I don't know if it is intended. But it seems very hard for users to understand. Note in the #15104, it seems type parameter in generic type is checked co-variantly only when the type parameter ...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/16797
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Generic type parameter in plain function is checked contravariantly. ","articleBody":"I don't know if it is intended. But it seems very hard for users to understand. \r\n\r\nNote in the https://github.com/Microsoft/TypeScript/pull/15104, it seems type parameter in generic type is checked **co-variantly** only when the type parameter is used in the parameter position of a **callback** (that is, a contravariant position in another contravariant position makes a cavariant position). \r\n\r\nHowever, in the following example, `Done` is a plain function, not callback. So `A` is contravariantly checked. This might confuse old TS users.\r\n\r\n**TypeScript Version:** 2.4.1\r\n\r\n**Code**\r\n\r\n```ts\r\nclass Animal {\r\n private animalTag: any\r\n}\r\nclass Dog extends Animal {\r\n private dogTag: any\r\n}\r\nfunction trainDog(d: Dog) { }\r\ntype Done = \u003cA extends Animal>(result: A) => void\r\nfunction cloneAnimal(done: Done): void {}\r\nfunction cloneAnimal2(done: (result: Animal) => void): void {}\r\ncloneAnimal(trainDog); // error\r\ncloneAnimal2(trainDog); // compiles\r\n```\r\n\r\n**Expected behavior:**\r\n\r\nAll compiles.\r\n\r\n**Actual behavior:**\r\n\r\n`cloneAnimal` fails to compile. `Argument of type '(d: Dog) => void' is not assignable to parameter of type 'Done'. Types of parameters 'd' and 'result' are incompatible. Type 'A' is not assignable to type 'Dog'. Type 'Animal' is not assignable to type 'Dog'. Property 'dogTag' is missing in type 'Animal'.`\r\n\r\nRelated: \r\nhttps://github.com/Microsoft/TypeScript/issues/16795\r\nhttps://github.com/Microsoft/TypeScript/issues/16790","author":{"url":"https://github.com/HerringtonDarkholme","@type":"Person","name":"HerringtonDarkholme"},"datePublished":"2017-06-28T10:08:46Z","url":"https://github.com/microsoft/TypeScript/issues/16797"}
| 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:44c81141-cdb2-56fd-1a23-59b380627914 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9AD4:2E5652:8B7608:B2E46B:6B00EBB7 |
| html-safe-nonce | b8f26987c8a4aea296046fd218c1dce283a4710c514c88f8712c289f4b9eb465 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5QUQ0OjJFNTY1Mjo4Qjc2MDg6QjJFNDZCOjZCMDBFQkI3IiwidmlzaXRvcl9pZCI6IjYyMzQyNzAzMTYxMzUyNDY3NzUiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 15cdbe0a47dc535ae0e5341bbbdeedf11f57967ed38d24ae864423991605d7b3 |
| hovercard-subject-tag | issue:239115324 |
| 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/16797/issue_layout |
| twitter:image | https://opengraph.githubassets.com/5b642e6939b7d1561be37e49947fb74111ead528b8fa7e25b99b12b66b19d898/microsoft/TypeScript/issues/16797 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/5b642e6939b7d1561be37e49947fb74111ead528b8fa7e25b99b12b66b19d898/microsoft/TypeScript/issues/16797 |
| og:image:alt | I don't know if it is intended. But it seems very hard for users to understand. Note in the #15104, it seems type parameter in generic type is checked co-variantly only when the type parameter is u... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | HerringtonDarkholme |
| 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