Title: Generic function argument lost when passed to a generic identity function · Issue #38007 · microsoft/TypeScript · GitHub
Open Graph Title: Generic function argument lost when passed to a generic identity function · Issue #38007 · microsoft/TypeScript
X Title: Generic function argument lost when passed to a generic identity function · Issue #38007 · microsoft/TypeScript
Description: TypeScript Version: 3.8.3 Search Terms: generic function "instantiated with a different subtype of constraint" Expected behavior: In TypeScript 3.7.5, the function returned by define is of type
Open Graph Description: TypeScript Version: 3.8.3 Search Terms: generic function "instantiated with a different subtype of constraint" Expected behavior: In TypeScript 3.7.5, the function returned by define is of type
X Description: TypeScript Version: 3.8.3 Search Terms: generic function "instantiated with a different subtype of constraint" Expected behavior: In TypeScript 3.7.5, the function returned by define is o...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/38007
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Generic function argument lost when passed to a generic identity function","articleBody":"**TypeScript Version:** 3.8.3\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* generic function\r\n* \"instantiated with a different subtype of constraint\"\r\n\r\n**Expected behavior:**\r\nIn TypeScript 3.7.5, the function returned by `define` is of type `\u003cE>(argument: E, somethingElse: { key: E; }) => (a: string) => number`.\r\n\r\n**Actual behavior:**\r\nIn TypeScript 3.8.3, the function returned by `define` is of type `(argument: E, somethingElse: { key: E; }) => (a: string) => number`.\r\n\r\nI believe this behavior is a regression, since a function with generic argument types without itself being generic doesn't really make sense, and the coded did work with TS 3.7. My assumption here is that TS doesn't understand the two `E`s actually _must_ be the same type.\r\n\r\n**Code**\r\n```ts\r\n// In reality there are more arguments of an Action and they have complex types\r\ntype Action\u003cR> = (a: string) => R;\r\n\r\n// The purpose of this method is to allow definitions of action creators without explicitly typing an\r\n// Action's argument types every time\r\nfunction define\u003cC extends (...args: any[]) => Action\u003cany>>(creator: C): C {\r\n return creator;\r\n}\r\n\r\n// Type inferred in TS 3.7.5: \u003cE>(argument: E, somethingElse: { key: E; }) => (a: string) => number\r\n// Type inferred in TS 3.8.3: (argument: E, somethingElse: { key: E; }) => (a: string) => number\r\nconst definedAction = define(\r\n \u003cE>(\r\n argument: E,\r\n somethingElse: { key: E }\r\n ) => (a /* a has a properly inferred type */) => 5,\r\n);\r\n\r\nconst extra = { test: \"\" };\r\n\r\ndefinedAction(extra, { key: extra });\r\n/*\r\nError only in TS 3.8.3:\r\nArgument of type '{ test: string; }' is not assignable to parameter of type 'E'.\r\n '{ test: string; }' is assignable to the constraint of type 'E', but 'E' could be instantiated with a different subtype of constraint '{}'.(2345)\r\n*/\r\n\r\n```\r\n\r\n\r\n\r\n\u003cdetails>\u003csummary>\u003cb>Output\u003c/b>\u003c/summary>\r\n\r\n```ts\r\n\"use strict\";\r\n// The purpose of this method is to allow definitions of action creators without explicitly typing an\r\n// Action's argument types every time\r\nfunction define(creator) {\r\n return creator;\r\n}\r\n// Type inferred in TS 3.7.5: \u003cE>(argument: E, somethingElse: { key: E; }) => (a: string) => number\r\n// Type inferred in TS 3.8.3: (argument: E, somethingElse: { key: E; }) => (a: string) => number\r\nconst definedAction = define((argument, somethingElse) => (a /* a has a properly inferred type */) => 5);\r\nconst extra = { test: \"\" };\r\ndefinedAction(extra, { key: extra });\r\n/*\r\nError only in TS 3.8.3:\r\nArgument of type '{ test: string; }' is not assignable to parameter of type 'E'.\r\n '{ test: string; }' is assignable to the constraint of type 'E', but 'E' could be instantiated with a different subtype of constraint '{}'.(2345)\r\n*/\r\n\r\n```\r\n\r\n\r\n\u003c/details>\r\n\r\n\r\n\u003cdetails>\u003csummary>\u003cb>Compiler Options\u003c/b>\u003c/summary>\r\n\r\n```json\r\n{\r\n \"compilerOptions\": {\r\n \"noImplicitAny\": true,\r\n \"strictNullChecks\": true,\r\n \"strictFunctionTypes\": true,\r\n \"strictPropertyInitialization\": true,\r\n \"strictBindCallApply\": true,\r\n \"noImplicitThis\": true,\r\n \"noImplicitReturns\": true,\r\n \"useDefineForClassFields\": false,\r\n \"alwaysStrict\": true,\r\n \"allowUnreachableCode\": false,\r\n \"allowUnusedLabels\": false,\r\n \"downlevelIteration\": false,\r\n \"noEmitHelpers\": false,\r\n \"noLib\": false,\r\n \"noStrictGenericChecks\": false,\r\n \"noUnusedLocals\": false,\r\n \"noUnusedParameters\": false,\r\n \"esModuleInterop\": true,\r\n \"preserveConstEnums\": false,\r\n \"removeComments\": false,\r\n \"skipLibCheck\": false,\r\n \"checkJs\": false,\r\n \"allowJs\": false,\r\n \"declaration\": true,\r\n \"experimentalDecorators\": false,\r\n \"emitDecoratorMetadata\": false,\r\n \"target\": \"ES2017\",\r\n \"module\": \"ESNext\"\r\n }\r\n}\r\n```\r\n\r\n\r\n\u003c/details>\r\n\r\n**Playground Link:** [Provided](https://www.typescriptlang.org/play/index.html?ssl=1&ssc=1&pln=27&pc=1#code/PTAEEkDtQJwUwIYBsCWAXAnqNALO9QECBbAewKIHMBXYuSNAZ1FIDNDoBBAYzRVOgJIAE2x4sOBADc4obqWIAHJHAAe2DIriMAUJi2gefAQB4ASgD5QAXlAAKBAC5QjNDBSRKAShtWzAbh0dEFAAFTxQRWoYRVJGWTYxFGY6XFJRZOxSQiQkUgB3UGE4Vg90fkhmRIReCrl4BDRyZnz0HFJqNFA1ZRRudCQsfQ9KDmCwIwqAcmYqWnou-W1umRghlDodVmpIWoEiko84EwBhbtU0emFmOwA6e6pGZyEMAG0AXR9rK0nTF4sLHZuA0mjBnCcvODQABvHSgeGwOBoaLQYGIUGBAC+QRCoU0sg8rHw8Ay0FCAGVQABmW4AdluAFZnCYAKKAuZ0BjOFkAGhcCiROBGLKQ8Wc0NAAGs4Bhuf5QJivlYHM5XO5PErQJBaAAjfDjML40CE4lwUlhSk0gActypzgcMBonLQ3L5jAFuGForg4qlMrlCs1KpcbhGmu1xD1MB08kqXWKpUgZt+0FsCaOdjhCNZgKzCIRHIWrrz+f5qSFnhFYphftloBZCpLQYQoGAACpCKBJLNIjBSFoYINjZAiTASRoDG3gJqGTydF5AjGBK5zm4W7YJZdXM4AEQ7hWL9NJ4QpuxqNd8iXSuvnmAtxWBds6Flj8gsSBDjwW6m3G12nScI68wMCw7BLKAUybtoLohuqlDypiUzGswkCkF0CCMIwKCUJACA6ioWSREQCCpPgoETrIUwslMtxZpB2DQaqoaeAhSGZBhWE4XhBFNGIsixmqCAeF0iTgVRUx8jqnQQdRcgdEgoh6sOrhCHwjRmqArS4J2wgoKwo4LC41A6uBiQCWuwkQdCiG3HYABMVIACwMl4OhTjoQA)\r\n\r\n**Related issues:**\r\n* #30134","author":{"url":"https://github.com/invliD","@type":"Person","name":"invliD"},"datePublished":"2020-04-16T15:31:45Z","url":"https://github.com/microsoft/TypeScript/issues/38007"}
| 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:1a334e0d-ffc7-fccf-5f74-5543e72a6e12 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BFA0:136CDA:9138F7:C0E0D0:6B01B4F7 |
| html-safe-nonce | 6321cfbfb088c07139f7ad7b94295a7c0745ea2f5593b48ce5f1932fba7109a4 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCRkEwOjEzNkNEQTo5MTM4Rjc6QzBFMEQwOjZCMDFCNEY3IiwidmlzaXRvcl9pZCI6IjI2OTQzMjAyMTMwNjY4ODg0MzkiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 2798864f0e5096ccbd848460ed6c0f74be2ec0cc217ccf348c42f81a413527b2 |
| hovercard-subject-tag | issue:601169474 |
| 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/38007/issue_layout |
| twitter:image | https://opengraph.githubassets.com/046d726462d0d398d7490e995c1ed1fb295b5976c7da85263ff4497bba80d083/microsoft/TypeScript/issues/38007 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/046d726462d0d398d7490e995c1ed1fb295b5976c7da85263ff4497bba80d083/microsoft/TypeScript/issues/38007 |
| og:image:alt | TypeScript Version: 3.8.3 Search Terms: generic function "instantiated with a different subtype of constraint" Expected behavior: In TypeScript 3.7.5, the function returned by define is of type |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | invliD |
| 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