Title: Unexpected behavior of generic constraints · Issue #3410 · microsoft/TypeScript · GitHub
Open Graph Title: Unexpected behavior of generic constraints · Issue #3410 · microsoft/TypeScript
X Title: Unexpected behavior of generic constraints · Issue #3410 · microsoft/TypeScript
Description: The following code snippet compiles without type errors though the type variable T is constrained to string type. function foo1(f: (s: string) => string) { return f("hello world"); } function foo2(f: (s: number) => number) { return f(123...
Open Graph Description: The following code snippet compiles without type errors though the type variable T is constrained to string type. function foo1(f: (s: string) => string) { return f("hello world"); } function foo2(...
X Description: The following code snippet compiles without type errors though the type variable T is constrained to string type. function foo1(f: (s: string) => string) { return f("hello world"); } f...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/3410
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Unexpected behavior of generic constraints","articleBody":"The following code snippet compiles without type errors though the type variable `T` is constrained to `string` type. \n\n``` typescript\n\nfunction foo1(f: (s: string) => string)\n{\n return f(\"hello world\");\n}\n\nfunction foo2(f: (s: number) => number)\n{\n return f(123);\n}\n\nfunction genericBar\u003cT extends string>(arg: T): T\n{\n return arg;\n}\n\nvar x1 = foo1(genericBar);\nvar x2 = foo2(genericBar);\n```\n\nNon generic version `nonGenericBar` works as expected.\n\n``` typescript\n\nfunction nonGenericBar(arg: string)\n{\n return arg;\n}\n\nvar y1 = foo1(nonGenericBar);\nvar y2 = foo2(nonGenericBar); // Type error\n```\n\nOf course, `genericBar` function is useless because constraining a type variable to a primitive type can be replaced by a non generic function like `nonGenericBar`. However, the behaviour is somewhat unexpected and inconsistent.\n","author":{"url":"https://github.com/kseo","@type":"Person","name":"kseo"},"datePublished":"2015-06-08T03:08:17Z","url":"https://github.com/microsoft/TypeScript/issues/3410"}
| 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:28b92ff6-63f8-2b3d-47a1-15a877564f73 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E468:1DB641:85F333:AEE089:6B00DA05 |
| html-safe-nonce | 22c8c179e6991d0db66ca65d8fe06fa30ed8527f7ef62b65128e39e3c1aa87f2 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFNDY4OjFEQjY0MTo4NUYzMzM6QUVFMDg5OjZCMDBEQTA1IiwidmlzaXRvcl9pZCI6IjY5Njc0NTgwNzE0OTY5NDAwMzciLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 6b333df68bffc082edd01b8ab5f68b6cd1865fe5ccf0be4fa9d521586314775d |
| hovercard-subject-tag | issue:86003569 |
| 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/3410/issue_layout |
| twitter:image | https://opengraph.githubassets.com/70031eda9b97f9a7b86c803005e4e30461318a32649d753984cc40635d9492b1/microsoft/TypeScript/issues/3410 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/70031eda9b97f9a7b86c803005e4e30461318a32649d753984cc40635d9492b1/microsoft/TypeScript/issues/3410 |
| og:image:alt | The following code snippet compiles without type errors though the type variable T is constrained to string type. function foo1(f: (s: string) => string) { return f("hello world"); } function foo2(... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | kseo |
| hostname | github.com |
| expected-hostname | github.com |
| None | a481f62a1e04c9b9bdcb5628230682c66d59006f5b13b080f79cba84a176905b |
| 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 | 84c8c5cc25e2fb7f2308428458538436baecc6d2 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width