Title: Sub-optimal type parameter inference with strictFunctionTypes enabled · Issue #52111 · microsoft/TypeScript · GitHub
Open Graph Title: Sub-optimal type parameter inference with strictFunctionTypes enabled · Issue #52111 · microsoft/TypeScript
X Title: Sub-optimal type parameter inference with strictFunctionTypes enabled · Issue #52111 · microsoft/TypeScript
Description: These are cases I've extracted from #49929; they are cases where if you don't specify the type parameters yourself, the inference isn't good. This is potentially a full duplicate of #49924, however, the fix suggested in that issue does n...
Open Graph Description: These are cases I've extracted from #49929; they are cases where if you don't specify the type parameters yourself, the inference isn't good. This is potentially a full duplicate of #49924, however...
X Description: These are cases I've extracted from #49929; they are cases where if you don't specify the type parameters yourself, the inference isn't good. This is potentially a full duplicate of #49...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/52111
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Sub-optimal type parameter inference with strictFunctionTypes enabled","articleBody":"These are cases I've extracted from #49929; they are cases where if you don't specify the type parameters yourself, the inference isn't good.\r\n\r\nThis is potentially a full duplicate of #49924, however, the fix suggested in that issue does not work properly in our codebase. To test these, it may be easiest to just clone https://github.com/microsoft/TypeScript/pull/49929 with the explicit type parameters removed and see what works and what doesn't.\r\n\r\n\r\n```ts\r\nenum SyntaxKind {\r\n Block,\r\n Identifier,\r\n CaseClause,\r\n FunctionExpression,\r\n FunctionDeclaration,\r\n}\r\n\r\ninterface Node { kind: SyntaxKind; }\r\ninterface Expression extends Node { _expressionBrand: any; }\r\ninterface Declaration extends Node { _declarationBrand: any; }\r\ninterface Block extends Node { kind: SyntaxKind.Block; }\r\ninterface Identifier extends Expression, Declaration { kind: SyntaxKind.Identifier; }\r\ninterface CaseClause extends Node { kind: SyntaxKind.CaseClause; }\r\ninterface FunctionDeclaration extends Declaration { kind: SyntaxKind.FunctionDeclaration; }\r\n\r\ntype HasLocals = Block | FunctionDeclaration;\r\ndeclare function canHaveLocals(node: Node): node is HasLocals;\r\n\r\ndeclare function assertNode\u003cT extends Node, U extends T\u003e(node: T | undefined, test: (node: T) =\u003e node is U): asserts node is U;\r\ndeclare function assertNode(node: Node | undefined, test: ((node: Node) =\u003e boolean) | undefined): void;\r\n\r\nfunction foo(node: FunctionDeclaration | CaseClause) {\r\n assertNode(node, canHaveLocals)\r\n\r\n node;\r\n // ^?\r\n\r\n assertNode\u003cNode, HasLocals\u003e(node, canHaveLocals)\r\n\r\n node;\r\n // ^?\r\n}\r\n\r\n\r\ndeclare function isExpression(node: Node): node is Expression;\r\n\r\ndeclare function cast\u003cTOut extends TIn, TIn = any\u003e(value: TIn | undefined, test: (value: TIn) =\u003e value is TOut): TOut;\r\n\r\nfunction bar(node: Identifier | FunctionDeclaration) {\r\n const a = cast(node, isExpression);\r\n // ^?\r\n\r\n const b = cast\u003cExpression\u003e(node, isExpression);\r\n // ^?\r\n}\r\n```\r\n\r\n\r\n\r\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eOutput\u003c/b\u003e\u003c/summary\u003e\r\n\r\n```ts\r\n\"use strict\";\r\nvar SyntaxKind;\r\n(function (SyntaxKind) {\r\n SyntaxKind[SyntaxKind[\"Block\"] = 0] = \"Block\";\r\n SyntaxKind[SyntaxKind[\"Identifier\"] = 1] = \"Identifier\";\r\n SyntaxKind[SyntaxKind[\"CaseClause\"] = 2] = \"CaseClause\";\r\n SyntaxKind[SyntaxKind[\"FunctionExpression\"] = 3] = \"FunctionExpression\";\r\n SyntaxKind[SyntaxKind[\"FunctionDeclaration\"] = 4] = \"FunctionDeclaration\";\r\n})(SyntaxKind || (SyntaxKind = {}));\r\nfunction foo(node) {\r\n assertNode(node, canHaveLocals);\r\n node;\r\n // ^?\r\n assertNode(node, canHaveLocals);\r\n node;\r\n // ^?\r\n}\r\nfunction bar(node) {\r\n const a = cast(node, isExpression);\r\n // ^?\r\n const b = cast(node, isExpression);\r\n // ^?\r\n}\r\n\r\n```\r\n\r\n\r\n\u003c/details\u003e\r\n\r\n\r\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eCompiler Options\u003c/b\u003e\u003c/summary\u003e\r\n\r\n```json\r\n{\r\n \"compilerOptions\": {\r\n \"strict\": true,\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 \"alwaysStrict\": true,\r\n \"esModuleInterop\": true,\r\n \"declaration\": true,\r\n \"experimentalDecorators\": true,\r\n \"emitDecoratorMetadata\": true,\r\n \"target\": \"ES2017\",\r\n \"jsx\": \"react\",\r\n \"module\": \"ESNext\",\r\n \"moduleResolution\": \"node\"\r\n }\r\n}\r\n```\r\n\r\n\r\n\u003c/details\u003e\r\n\r\n**Playground Link:** [Provided](https://www.typescriptlang.org/play?#code/KYOwrgtgBAygniALgQwB4GkCWIAmUDeAUFCVAEIA2A9gMYDWANMaQJI6iKYBmmwATk1JQAwsgDOwYRWRgJg0gDEwIGpyogAoqgAOfYGLGZ18kkpVqQAEWA1pfZBaYBfQoWyJ+XZDWBQAclTsBFB02DgAXLAIKBhhANxQLu6e3r5auvqG6lDAqB64Yv6BvvhQAPq5GQZGIGT2uJHIIHAJSUgpPlDWtsj2Fjl5oDiFAUGlZew9fTV1TRFQTS2Jbu18Xp2UtHQD+cNFYyFhkfBIaFi4AHSb9K0rHmupUGwc3Lx8O0OF6XrVxl02dgcNWCoQaUVOsUuzyQr34t2SD06ogkUhkEg+BX2JUOYJOMXOOAuyMk0lkwHhq3WvjMqhq3UB-Vyu0K9N6QOypVB8zxZzCFxpFlZ03Ut0IiDg2l8AAlxAAZWjICiFAC85Go9CgAB8oAK6QC2RY4oRJnZfFxlLTsjQmjKAG7AeXWpUAChAxUio2AAEpIm6gphCjKxI7FWIjcb9XooObzMDxBI+IhPQAeAAqGL2noYUAAqhnCqmAHyu91QdPa5TsHggYA4bMeMSISIl9iRVNeqDKwtQP2+AO5n0LAz8RCFXtQfs5o0m3pmi39eMjz0t4Ae4paqCV4DV2v1-RNqDOldr9gdrtQABGVCoFGATQ7Fdw2+wtcHtqomBw4ZjlpA0evx46vOepTOyf7asSqJkh2RBCIuibLr22bWiAdoOgqSpeq4Qi9kaQgAPT4VAAB6AD82GkPBSbFMmWZQEGIZKsWSFQChaGMWIWHMCQuHcVAhEkeRLiuDOUY-v0AbfJkNSAZ6g7jv2Um-CA4aiXOsZWuIiBpgA8mAiD5mWLAgNmqbGZ2CzNMWtqKmAq5GeBm5PjudZQA2B7OjZFB2W2xlnt2Xl2ROBZ6Ygg6pqF37AdkF69IB0KcDw-Abrq6hCmBMF8TQ6iNgsFnWo2K7ZpJOg-FkIBenhpACaQZEUSQ2UgLlF75VpyZKeVzHFMVYgdTUlV8TVJB1U4QA)\r\n ","author":{"url":"https://github.com/jakebailey","@type":"Person","name":"jakebailey"},"datePublished":"2023-01-05T00:10:41.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":3},"url":"https://github.com/52111/TypeScript/issues/52111"}
| 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:39254a07-6af5-b703-2bba-8aacbc3b55db |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E3C2:1175AA:3EE0AC:52BE8D:6A614E27 |
| html-safe-nonce | 0058794acfba8b04612df4366f7c3b6b71bf8411745b9ec9cd0a9d920d2176a9 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFM0MyOjExNzVBQTozRUUwQUM6NTJCRThEOjZBNjE0RTI3IiwidmlzaXRvcl9pZCI6IjQyMDk3OTc1ODUyMDM5NzM2NzEiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 0ea3ea4493b7e4bb26a425dd722427509d32fcaf3994790c58742fd9d1a344b8 |
| hovercard-subject-tag | issue:1519766410 |
| 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/52111/issue_layout |
| twitter:image | https://opengraph.githubassets.com/c36c8fbf3f4820c399ec889f1f6ff8d69efe367a116e0352b9351a92995889d3/microsoft/TypeScript/issues/52111 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/c36c8fbf3f4820c399ec889f1f6ff8d69efe367a116e0352b9351a92995889d3/microsoft/TypeScript/issues/52111 |
| og:image:alt | These are cases I've extracted from #49929; they are cases where if you don't specify the type parameters yourself, the inference isn't good. This is potentially a full duplicate of #49924, however... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | jakebailey |
| hostname | github.com |
| expected-hostname | github.com |
| None | 35b919fdb8e6752d2ed95a144e761ca5e924557563e2ed0a5ebf6c4430cdeeb6 |
| 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 | 1e22b08b60ccb67c3791f565bed248565260e611 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width