Title: Add a strict flag forbidding impossible generic declarations · Issue #26129 · microsoft/TypeScript · GitHub
Open Graph Title: Add a strict flag forbidding impossible generic declarations · Issue #26129 · microsoft/TypeScript
X Title: Add a strict flag forbidding impossible generic declarations · Issue #26129 · microsoft/TypeScript
Description: Search Terms generic declaration return type parameter Suggestion Today you can write declarations like this: function getThing
Open Graph Description: Search Terms generic declaration return type parameter Suggestion Today you can write declarations like this: function getThing
X Description: Search Terms generic declaration return type parameter Suggestion Today you can write declarations like this: function getThing<T>(): T { return null as any as T; // ¯\_(ツ)_/¯ } const x = get...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/26129
X: @github
Domain: github.com
IPv4 address: 140.82.112.4
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Add a strict flag forbidding impossible generic declarations","articleBody":"## Search Terms\r\n\r\ngeneric declaration return type parameter\r\n\r\n## Suggestion\r\n\r\nToday you can write declarations like this:\r\n```ts\r\nfunction getThing\u003cT>(): T {\r\n return null as any as T; // ¯\\_(ツ)_/¯\r\n}\r\nconst x = getThing\u003c\"cool\">(); // Type safety!\r\n```\r\n\r\nThis isn't particularly meaningful - `getThing` has no plausible correct implementation that doesn't rely on the caller asking for the correct type by coincidence.\r\n\r\nWe then also started allowing this to enable some other scenarios (#16072):\r\n```ts\r\n// This will return a number because I wished for it\r\nconst p: number = getThing();\r\n```\r\nSee also https://github.com/Microsoft/TypeScript/issues/5256#issuecomment-148210025\r\n\r\nThe rule should be: *All type parameters must appear in an input position*; this would need to be under some flag like `--strictTypeParameterDeclarations` (please bikeshed).\r\n\r\nUsers should return `any` or `unknown` instead in these cases.\r\n\r\nA lint rule approximating this can be done syntactically so we should scan DefinitelyTyped to see what the breakage would look like.\r\n\r\n## Use Cases\r\n\r\nMostly to allow us to clean up DefinitelyTyped.\r\n\r\n## Examples\r\n\r\n\u003c!-- Show how this would be used and what the behavior would be -->\r\n\r\n## Checklist\r\n\r\nMy suggestion meets these guidelines:\r\n* [ ] This wouldn't be a breaking change in existing TypeScript / JavaScript code\r\n* [x] This wouldn't change the runtime behavior of existing JavaScript code\r\n* [x] This could be implemented without emitting different JS based on the types of the expressions\r\n* [x] This isn't a runtime feature (e.g. new expression-level syntax)\r\n\r\n","author":{"url":"https://github.com/RyanCavanaugh","@type":"Person","name":"RyanCavanaugh"},"datePublished":"2018-08-01T21:38:04Z","url":"https://github.com/microsoft/TypeScript/issues/26129"}
| 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:8f83f1fb-5cb7-d74d-9688-5a0ea03ad8ee |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | EDE2:9136E:3BB945:51E658:6B0451A5 |
| html-safe-nonce | edac41dfd42baf1086d3bc5fbe098934ab24cdcc9df7576a538a8706136794a1 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFREUyOjkxMzZFOjNCQjk0NTo1MUU2NTg6NkIwNDUxQTUiLCJ2aXNpdG9yX2lkIjoiNjY1OTYxODM2MDczMDYwMzk0MSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | b5b10d319bba8ec9830f6c4d4e2aa146d995a306cb26ac30602738c72732fbaf |
| hovercard-subject-tag | issue:346779045 |
| 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/26129/issue_layout |
| twitter:image | https://opengraph.githubassets.com/85bfd4a5a16e86f5d252d662539f750d7c54a9b04da293c92bf9c70f95470d79/microsoft/TypeScript/issues/26129 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/85bfd4a5a16e86f5d252d662539f750d7c54a9b04da293c92bf9c70f95470d79/microsoft/TypeScript/issues/26129 |
| og:image:alt | Search Terms generic declaration return type parameter Suggestion Today you can write declarations like this: function getThing |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | RyanCavanaugh |
| hostname | github.com |
| expected-hostname | github.com |
| None | 910ca0e260cfec6aff11407d8dc6a0d3a4cd381a2ec9ada56627b063426892a0 |
| 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 | e086f9b7e88322eb2b41885a6ad425b215737fa5 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width