Title: `asserts x is type` doesn't work when using arrow functions · Issue #34523 · microsoft/TypeScript · GitHub
Open Graph Title: `asserts x is type` doesn't work when using arrow functions · Issue #34523 · microsoft/TypeScript
X Title: `asserts x is type` doesn't work when using arrow functions · Issue #34523 · microsoft/TypeScript
Description: TypeScript Version: 3.7.0-dev.20191016 Search Terms: assertion signatures, asserts, asserts is type Code function isString1(val: any): asserts val is string { if (typeof val !== "string") throw "Nope" } const isString2 = (val: any): asse...
Open Graph Description: TypeScript Version: 3.7.0-dev.20191016 Search Terms: assertion signatures, asserts, asserts is type Code function isString1(val: any): asserts val is string { if (typeof val !== "string") throw "No...
X Description: TypeScript Version: 3.7.0-dev.20191016 Search Terms: assertion signatures, asserts, asserts is type Code function isString1(val: any): asserts val is string { if (typeof val !== "string")...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/34523
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`asserts x is type` doesn't work when using arrow functions","articleBody":"\u003c!-- Please try to reproduce the issue with `typescript@next`. It may have already been fixed. -->\r\n**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:** assertion signatures, asserts, asserts is type\r\n\r\n**Code**\r\n\r\n```ts\r\nfunction isString1(val: any): asserts val is string {\r\n if (typeof val !== \"string\") throw \"Nope\"\r\n} \r\n\r\nconst isString2 = (val: any): asserts val is string => {\r\n if (typeof val !== \"string\") throw \"Nope\"\r\n} \r\n\r\nconst x: any = \"3\";\r\n\r\nisString1(x);\r\nisString2(x);\r\n\r\nx.toUpperCase();\r\n```\r\n\r\n**Expected behavior:**\r\n\r\nCompiles, infers that `x` is a string, both when using only `isString1` or `isString2`\r\n\r\n**Actual behavior:**\r\n\r\nCompile error on `isString2`:\r\n\r\n```\r\nAssertions require every name in the call target to be declared with an explicit type annotation.(2775)\r\n```\r\n\r\n**Playground Link:** [1⃣ playground](http://www.typescriptlang.org/play/?ts=Nightly&ssl=15&ssc=1&pln=1&pc=1#code/GYVwdgxgLglg9mABDAzgZSgJxmA5gRgAoA3AQwBsAuRUsATwEprSUUBTTKFRM85blFhy5EAbwBQiKcmCJCUOgAc2cWb0QBCALxbEAIkHY8ehoigALTHADu+gHJxle8QF9E48RASD+GI7gAmRF0SCmZ6JhpWDi4eCn5EQ2FggD4xSWkYWXklFTV47V0DIWNTCytbPQcnV3dPbyhEAA9wumD9AGY9AG4PVD9hIiaGXv6SwMJh3vEmgDooOABVRWVMAGEWNkIR8SA)\r\n\r\n**Related Issues:** #33743 \r\n\r\n**Context**\r\n\r\nIn principle the above problem is easy to work around, however, the result of it is when type checkers are stored on objects, the above error returns as is demonstrated in [2⃣ this playground](http://www.typescriptlang.org/play/?ts=Nightly#code/C4TwDgpgBAKuEGEAWEDGBrCAnAPDAfFALxQDeAUFFVABQBuAhgDYBcUDAdiAJRsMDO-bMH5RGTKAEtRMANzkAvuXIAzAK4dUwSQHsOUALYNMcSHnw1UegCZt6zPl27FCAIx06mETr1jxkaJi4BGSU1FgQwGpY+uqa2nq04o48odTpUiq0AIRWHNb2TNzOwEhYOgDuUBwQVQCiWOVYNABEAPI6YPwt3GFUCuyinCCKynn8wFL8AMrAWJIcAOZspogoGNg4E-NLhCRGJvA0hSnORISgkDpZ4sREJC3bC4s9Y3oTUKjrQfwAjMRkKazHaLKBKcaTL6BbD8ABMbFIQLmzxW-m+myeuzBAMR0mBzzB5Ahn3RWH4AGYEUiQWxLhBrtSCQMSLiZsiloTiVCNmSACw4xkcgYCT7vYBvDgfAAeKQBLXJLXk5Dx7MWNCl3Hk3J+vwAdCqQerNVAAPQmqD8JA6NRMazVHSTMDlaxqVDQKwGMCSLxQbBNImkuH6tmGjVawPk4P4pZG8PQvlR1Wx03my3W232x3O13unSe73QP06LDKKW64A6ACqYEgWAQAggNE15CAA). Note how the very same function fails when it is stored in an object, unless the type is provided explicitly.\r\n","author":{"url":"https://github.com/mweststrate","@type":"Person","name":"mweststrate"},"datePublished":"2019-10-16T20:24:00Z","url":"https://github.com/microsoft/TypeScript/issues/34523"}
| 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:9c04205b-1f63-1090-613f-6b2c12125455 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B684:1087D4:AB4818:E5B216:6B00F21C |
| html-safe-nonce | 86887c6c768bba2ff016ffaa3e7b24b825b96dce5f6eef5287e50eb8ac99bb9d |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCNjg0OjEwODdENDpBQjQ4MTg6RTVCMjE2OjZCMDBGMjFDIiwidmlzaXRvcl9pZCI6IjI5NTY1MDU4MDQ0NTYzOTExOTYiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 268e2b0a91f1471c0cab93e53cce1a89ef9bce267dc109c7249b2b53492c5a45 |
| hovercard-subject-tag | issue:508079201 |
| 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/34523/issue_layout |
| twitter:image | https://opengraph.githubassets.com/abb6862f809a17c8f5185e0b745440832e663cc51839d1a06ae61e567780ff91/microsoft/TypeScript/issues/34523 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/abb6862f809a17c8f5185e0b745440832e663cc51839d1a06ae61e567780ff91/microsoft/TypeScript/issues/34523 |
| og:image:alt | TypeScript Version: 3.7.0-dev.20191016 Search Terms: assertion signatures, asserts, asserts is type Code function isString1(val: any): asserts val is string { if (typeof val !== "string") throw "No... |
| 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