Title: TS 3.7: unlike `x is T`, `asserts x is T` cannot close over generics defined in outer scopes · Issue #34596 · microsoft/TypeScript · GitHub
Open Graph Title: TS 3.7: unlike `x is T`, `asserts x is T` cannot close over generics defined in outer scopes · Issue #34596 · microsoft/TypeScript
X Title: TS 3.7: unlike `x is T`, `asserts x is T` cannot close over generics defined in outer scopes · Issue #34596 · microsoft/TypeScript
Description: TypeScript Version: 3.7.0-dev.20191016 Search Terms: asserts, asserts return, asserts higher order, asserts type, 2775 Code function literal
Open Graph Description: TypeScript Version: 3.7.0-dev.20191016 Search Terms: asserts, asserts return, asserts higher order, asserts type, 2775 Code function literal
X Description: TypeScript Version: 3.7.0-dev.20191016 Search Terms: asserts, asserts return, asserts higher order, asserts type, 2775 Code function literal<T extends keyof any>(lit: T): { is(value: any): va...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/34596
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"TS 3.7: unlike `x is T`, `asserts x is T` cannot close over generics defined in outer scopes","articleBody":"**TypeScript Version:** 3.7.0-dev.20191016\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\nasserts, asserts return, asserts higher order, asserts type, 2775\r\n\r\n**Code**\r\n\r\n```ts\r\nfunction literal\u003cT extends keyof any>(lit: T): {\r\n is(value: any): value is T\r\n assert(value: any): asserts value is T\r\n} {\r\n return null as any; // implementation doesn't matter\r\n}\r\n\r\nconst isHi = literal(\"hi\")\r\nconst x: unknown = \"test\"\r\n\r\nif (isHi.is(x)) {\r\n console.log(x) // x is correctly inferred to be 'hi' :)\r\n}\r\n\r\nisHi.assert(x); // error: Assertions require every name in the call target to be declared with an explicit type annotation.(2775)\r\nconsole.log(x); // x should be inferred to be 'hi' here :(\r\n```\r\n\r\n**Expected behavior:**\r\n\r\nNo compile error, `x` is inferred to be `\"hi\"` on the last line.\r\n\r\n**Actual behavior:**\r\n\r\nCompile error on `isHi.assert`. `Assertions require every name in the call target to be declared with an explicit type annotation.(2775)`\r\n\r\nConstruction higher order type guards is possible without problem (as shown in the snippet). This mechanism used heavily in libraries like `io-ts` and `mobx-state-tree`. \r\n\r\nHowever, when trying to extend the latter library with assertion functionality for more convenient control flow, we run into this issue.\r\n\r\nWe can build `type.is` properly, but not `type.assert`, although they seem to be needing the exact same type / depth of type analysis; if type guards can close over `T`, so should type assertions?\r\n\r\n**Playground Link:** [link](http://www.typescriptlang.org/play/?ts=3.7-Beta#code/C4TwDgpgBAggznCAnYcA8AVAfFAvFACgDcBDAGwFcIAuKCgOwGt6B7Ad3oEo8cSFlUUUpWgBLOFAwAoKQDMGAY2CiW9KGVHBk5TFAgAPLfQAmExhBAtZUEvRBYCG4LQydaAbylRvUccXJUtLYgbkIBYhLSPjb8KP4iQXahfIgoEsJUvpFePgD0uTGpzrCxqJhYUgC+UJ7RSBDAFEhq9BRkZDE2dgDcVTIKqnDAWQASonjqmtpkBABEABais5z9g8P6tAzM7Gr4s1pDszKi1gTiYwB0fvqc3LU+A-RwLGQQF2QsAOYEN1D5UPoslABkh6koyCBfPRZMh6sYoMAWFAAOSLZF9KTnUQXFICH6cbp-AqwlhIKDUAirJ4vN4fb43Qn-QFweYsNrwgBGYmhsIg8L4KLRUBYRGQUHmyAgQA)\r\n\r\n**Related Issues:** \r\n\r\n#34523 \r\n\r\n","author":{"url":"https://github.com/mweststrate","@type":"Person","name":"mweststrate"},"datePublished":"2019-10-19T18:42:52Z","url":"https://github.com/microsoft/TypeScript/issues/34596"}
| 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:bceab72f-a7d7-2c7d-b82d-5146c20a20dc |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A3AC:14ECB:AD0DE9:DD6EC5:6B00F60C |
| html-safe-nonce | 437c3da74d105720bb8df987d74ebc1a7463c77ea6bbd7c32b337e13922e69be |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBM0FDOjE0RUNCOkFEMERFOTpERDZFQzU6NkIwMEY2MEMiLCJ2aXNpdG9yX2lkIjoiMzA4MzIxMzMxOTkxNjQ5NDAiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 4e876769c8c305d9104a9073ac04ad0b4f693efdbba9e4dea9105e704f620ea5 |
| hovercard-subject-tag | issue:509493451 |
| 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/34596/issue_layout |
| twitter:image | https://opengraph.githubassets.com/7672cad8a204da6391bb387d3e3deef3d74f04d81988cd35e6901952ed1aff7d/microsoft/TypeScript/issues/34596 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/7672cad8a204da6391bb387d3e3deef3d74f04d81988cd35e6901952ed1aff7d/microsoft/TypeScript/issues/34596 |
| og:image:alt | TypeScript Version: 3.7.0-dev.20191016 Search Terms: asserts, asserts return, asserts higher order, asserts type, 2775 Code function literal |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | mweststrate |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4ef498cbabcbe3c50befc7a5b743e7072ee770b4daf0e15e54801180d1fa91fc |
| 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 | 9983dd5da06943f833c971dd5b6bc988e2858be5 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width