Title: Referencing ReturnType
Open Graph Title: Referencing ReturnType
X Title: Referencing ReturnType
Description: TypeScript Version: 3.3.0-dev.20181129 Search Terms: ReturnType, Generic Param, Generic Function Argument Code //Innocent enough. //Take a T, return a T type Foo
Open Graph Description: TypeScript Version: 3.3.0-dev.20181129 Search Terms: ReturnType, Generic Param, Generic Function Argument Code //Innocent enough. //Take a T, return a T type Foo
X Description: TypeScript Version: 3.3.0-dev.20181129 Search Terms: ReturnType, Generic Param, Generic Function Argument Code //Innocent enough. //Take a T, return a T type Foo<T extends number> = (t: T) =&...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/29133
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Referencing ReturnType\u003cFooT> in generic param, and passing a function that has one param causes incorrect type inference...","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.3.0-dev.20181129\r\n\r\n\u003c!-- Search terms you tried before logging this (so others can find this issue more easily) -->\r\n**Search Terms:** ReturnType, Generic Param, Generic Function Argument\r\n\r\n**Code**\r\n\r\n```ts\r\n//Innocent enough.\r\n//Take a T, return a T\r\ntype Foo\u003cT extends number> = (t: T) => T;\r\n\r\n//=== NOT using ReturnType\u003cFooT> in Generic Parameters ==\r\ndeclare function bar\u003c\r\n T extends number,\r\n FooT extends Foo\u003cT>\r\n>(t: T, foo: FooT): ReturnType\u003cFooT>\r\n//OK! `barResult` is of type `3`\r\nconst barResult = bar(\r\n 3 as (3|5),\r\n () => 3\r\n);\r\n//OK! `barResult2` is of type `3`\r\nconst barResult2 = bar(\r\n 3 as (3|5),\r\n //`t` is of type `3|5`\r\n //Return type is 3 \r\n t => 3\r\n);\r\n\r\n//=== Using ReturnType\u003cFooT> in Generic Parameters ==\r\ndeclare function baz\u003c\r\n T extends number,\r\n FooT extends Foo\u003cT>,\r\n //The same as bar\u003c> but we reference ReturnType\u003cFooT> here\r\n Ret extends ReturnType\u003cFooT>\r\n>(t: T, foo: FooT, ret: Ret): ReturnType\u003cFooT>\r\n//OK! `bazResult` is of type `3`\r\nconst bazResult = baz(\r\n 3 as (3|5),\r\n () => 3,\r\n 3\r\n);\r\n//NOT OK! `bazResult2` is of type `3|5`!\r\nconst bazResult2 = baz(\r\n 3 as (3|5),\r\n //`t` is of type `3|5`\r\n //Return type is `3`\r\n t => 3,\r\n 3\r\n);\r\n\r\n//NOT OK! `bazResult3` is of type `3|5`!\r\nconst bazResult3 = baz(\r\n 3 as (3|5),\r\n //`t` is of type `3|5`\r\n //Return type is `3`\r\n //We *force* the return type to be 3\r\n (t) : 3 => (3 as 3),\r\n 3\r\n);\r\n```\r\n\r\n**Expected behavior:**\r\n\r\n`bazResult2` and `bazResult3` should be of type `3`, the same as `bazResult`\r\n\r\n**Actual behavior:**\r\n\r\n`bazResult2` and `bazResult3` are of type `3|5`, different from `bazResult` (which is of type `3`)\r\n\r\nThe difference between `bazResult` and `bazResult2` is literally 3 characters but it causes the inferred types to be very different.\r\n\r\n**Playground Link:** [Here](http://www.typescriptlang.org/play/#src=%2F%2FInnocent%20enough.%0A%2F%2FTake%20a%20T%2C%20return%20a%20T%0Atype%20Foo%3CT%20extends%20number%3E%20%3D%20(t%3A%20T)%20%3D%3E%20T%3B%0A%0A%2F%2F%3D%3D%3D%20NOT%20using%20ReturnType%3CFooT%3E%20in%20Generic%20Parameters%20%3D%3D%0Adeclare%20function%20bar%3C%0A%20%20%20%20T%20extends%20number%2C%0A%20%20%20%20FooT%20extends%20Foo%3CT%3E%0A%3E(t%3A%20T%2C%20foo%3A%20FooT)%3A%20ReturnType%3CFooT%3E%0A%2F%2FOK!%20%60barResult%60%20is%20of%20type%20%603%60%0Aconst%20barResult%20%3D%20bar(%0A%20%20%20%203%20as%20(3%7C5)%2C%0A%20%20%20%20()%20%3D%3E%203%0A)%3B%0A%2F%2FOK!%20%60barResult2%60%20is%20of%20type%20%603%60%0Aconst%20barResult2%20%3D%20bar(%0A%20%20%20%203%20as%20(3%7C5)%2C%0A%20%20%20%20%2F%2F%60t%60%20is%20of%20type%20%603%7C5%60%0A%20%20%20%20%2F%2FReturn%20type%20is%203%20%0A%20%20%20%20t%20%3D%3E%203%0A)%3B%0A%0A%2F%2F%3D%3D%3D%20Using%20ReturnType%3CFooT%3E%20in%20Generic%20Parameters%20%3D%3D%0Adeclare%20function%20baz%3C%0A%20%20%20%20T%20extends%20number%2C%0A%20%20%20%20FooT%20extends%20Foo%3CT%3E%2C%0A%20%20%20%20%2F%2FThe%20same%20as%20bar%3C%3E%20but%20we%20reference%20ReturnType%3CFooT%3E%20here%0A%20%20%20%20Ret%20extends%20ReturnType%3CFooT%3E%0A%3E(t%3A%20T%2C%20foo%3A%20FooT%2C%20ret%3A%20Ret)%3A%20ReturnType%3CFooT%3E%0A%2F%2FOK!%20%60bazResult%60%20is%20of%20type%20%603%60%0Aconst%20bazResult%20%3D%20baz(%0A%20%20%20%203%20as%20(3%7C5)%2C%0A%20%20%20%20()%20%3D%3E%203%2C%0A%20%20%20%203%0A)%3B%0A%2F%2FNOT%20OK!%20%60bazResult2%60%20is%20of%20type%20%603%7C5%60!%0Aconst%20bazResult2%20%3D%20baz(%0A%20%20%20%203%20as%20(3%7C5)%2C%0A%20%20%20%20%2F%2F%60t%60%20is%20of%20type%20%603%7C5%60%0A%20%20%20%20%2F%2FReturn%20type%20is%20%603%60%0A%20%20%20%20t%20%3D%3E%203%2C%0A%20%20%20%203%0A)%3B%0A%0A%2F%2FNOT%20OK!%20%60bazResult3%60%20is%20of%20type%20%603%7C5%60!%0Aconst%20bazResult3%20%3D%20baz(%0A%20%20%20%203%20as%20(3%7C5)%2C%0A%20%20%20%20%2F%2F%60t%60%20is%20of%20type%20%603%7C5%60%0A%20%20%20%20%2F%2FReturn%20type%20is%20%603%60%0A%20%20%20%20%2F%2FWe%20*force*%20the%20return%20type%20to%20be%203%0A%20%20%20%20(t)%20%3A%203%20%3D%3E%20(3%20as%203)%2C%0A%20%20%20%203%0A)%3B)\r\n\r\n-----\r\n\r\nI don't even know how to describe this...\r\n\r\nMy real use case is more complicated than the above example but I *need* the return type of a generic function to be inferred correctly to perform stronger compile-time type checks.\r\n\r\nThe strange thing is that `() => 3` will give me the correct return type, but `t => 3` will not.\r\nIn my own code, using the argument `t` is absolutely necessary.\r\n\r\nAs far as I've seen, this problem only occurs both the following are true,\r\n\r\n1. `ReturnType\u003cFooT>` is used in a generic param constraint\r\n2. The concrete type of `FooT` has parameters (Like `t => 3` has parameters, `() => 3` does not)\r\n\r\nIf either of the above are false, this problem does not occur","author":{"url":"https://github.com/AnyhowStep","@type":"Person","name":"AnyhowStep"},"datePublished":"2018-12-23T18:14:43Z","url":"https://github.com/microsoft/TypeScript/issues/29133"}
| 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:74d8f2a6-220d-208e-d7f1-3de68bbeacbf |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CD7C:3A4875:29D5F79:37CD9FE:6B01BCFA |
| html-safe-nonce | dd19a9590f1d5873b97a98b4f1bf94b3a111ace5f770438335520d3e2f7122f4 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDRDdDOjNBNDg3NToyOUQ1Rjc5OjM3Q0Q5RkU6NkIwMUJDRkEiLCJ2aXNpdG9yX2lkIjoiODQ4NzE1MjcyMzU3MjE0NTQwMiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 9a4399e08929ea77c1eba3af19a1767a729d50ebd1e41aab837c0fda3537d379 |
| hovercard-subject-tag | issue:393770312 |
| 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/29133/issue_layout |
| twitter:image | https://opengraph.githubassets.com/43f2d3ea7d8d509a8ad96d61d783d2cac504a999bc9a610c09f0911c883eba9f/microsoft/TypeScript/issues/29133 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/43f2d3ea7d8d509a8ad96d61d783d2cac504a999bc9a610c09f0911c883eba9f/microsoft/TypeScript/issues/29133 |
| og:image:alt | TypeScript Version: 3.3.0-dev.20181129 Search Terms: ReturnType, Generic Param, Generic Function Argument Code //Innocent enough. //Take a T, return a T type Foo |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | AnyhowStep |
| 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