Title: Assertion signature on generics doesn't narrow · Issue #60130 · microsoft/TypeScript · GitHub
Open Graph Title: Assertion signature on generics doesn't narrow · Issue #60130 · microsoft/TypeScript
X Title: Assertion signature on generics doesn't narrow · Issue #60130 · microsoft/TypeScript
Description: 🔎 Search Terms Assertion AND signature 🕗 Version & Regression Information This is the behavior in every version I tried, and I reviewed the FAQ for entries about Assertion Signatures, Generics. ⏯ Playground Link https://www.typescriptlan...
Open Graph Description: 🔎 Search Terms Assertion AND signature 🕗 Version & Regression Information This is the behavior in every version I tried, and I reviewed the FAQ for entries about Assertion Signatures, Generics. ⏯ P...
X Description: 🔎 Search Terms Assertion AND signature 🕗 Version & Regression Information This is the behavior in every version I tried, and I reviewed the FAQ for entries about Assertion Signatures, Generics....
Opengraph URL: https://github.com/microsoft/TypeScript/issues/60130
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Assertion signature on generics doesn't narrow","articleBody":"### 🔎 Search Terms\n\nAssertion AND signature\n\n### 🕗 Version & Regression Information\n\n- This is the behavior in every version I tried, and I reviewed the FAQ for entries about Assertion Signatures, Generics.\r\n\n\n### ⏯ Playground Link\n\nhttps://www.typescriptlang.org/play/?noUncheckedIndexedAccess=true&allowUnusedLabels=true&downlevelIteration=true&noEmitHelpers=true&noUnusedLocals=true&noUnusedParameters=true&preserveConstEnums=true&removeComments=true&importHelpers=true&target=99&useUnknownInCatchVariables=false&exactOptionalPropertyTypes=true&noImplicitOverride=true&noFallthroughCasesInSwitch=true&noPropertyAccessFromIndexSignature=true&inlineSourceMap=true&inlineSources=true&stripInternal=true&ts=5.6.2#code/FAehAICIHEHtYCbgIYGdUFMBOAXSAucAW2QGsNVwEKcsBXAYxzqwEsA7Ac3FUYYwwJQEAGZ12TVrHYp02HAAoGhcaXawA7uwCUhNJlyUG4AN7DwFi6xHgFAQgbbT5y6-A4AFlk3h2GDeAAKgCeAA4YAKJY3lgKAOQAgnK4UjIiyKwANoJx2gDcLpYAvuYl5pAAQshI+vIEVDT0TCwc3NgxwGISOKmyBjgAPADCAHxKhEO6ffJGzq7Wtg5OZm6Wnt4BfgEh4VEx8Un9velZOfnAriVlGAAeobC44F2S0u40CpkcFISotK0A2gBdKa-NhcIHOC6rcDZHBPcTKObQ1zsZBEDA-P5cArI1yfPyoTFgzhAnHQorgAA+vjomUy4AAvDS6QUoasRA9bAxpL8YV9wLAbPiKMs2biFgoALLITwAOiwyHYCFgRAUTgG4AADLKAKyi3HQ56MpEG5Go9GESAARw02EgABoxaa3MLCeB-sLAY7nZcyQaKRhMpgTT7argFM9zj7LNz2LyTOaMPa+QSKUznnlLGBwAMGXnwBEAEqFgDyhfAAAkixEnabXbLQnRUB4Pl8o9HpuHE+A7Pm4sgAEYMXJ+3ElZHj6ESyO11ZYDDMLBpBGy12j1zzxcyNdiopAA\n\n### 💻 Code\n\n```ts\r\n\r\n// \"Good assert\": makes destructuring succeed\r\n// function assert(c: unknown): asserts c {\r\n// if (!c) {\r\n// throw new TypeError('Assertion failed');\r\n// }\r\n// }\r\n\r\n// \"Bad assert\": destructuring error\r\nfunction assert\u003cC>(c: C): asserts c {\r\n if (!c) {\r\n throw new TypeError('Assertion failed');\r\n }\r\n}\r\n\r\nexport function test(lines: string[]): string[] {\r\n\r\n let func: {\r\n name: string;\r\n lines: string[];\r\n } | null = null;\r\n\r\n for (const line of lines) {\r\n if (Math.random() \u003c 0.5) {\r\n func = {\r\n name: \"qwer\",\r\n lines: [line],\r\n };\r\n } else {\r\n assert(func);\r\n const {name, lines} = func; // \u003c=== ERROR HERE: 'name' implicitly has type 'any'\r\n lines.push(line);\r\n assert(name !== 'abc');\r\n }\r\n }\r\n if (func)\r\n return func.lines;\r\n return lines;\r\n }\r\n```\r\n\n\n### 🙁 Actual behavior\n\n\r\nWhen the generic-assert implementation is active, the line -\r\n```ts\r\nconst {name, lines} = func;\r\n```\r\nerrors with `'name' implicitly has type 'any'`.\r\n\r\nWhen the `unknown` assert implementation is active, types are properly inferred.\n\n### 🙂 Expected behavior\n\nBoth `assert` versions contain assertion signature, and both should be able to narrow the type.\n\n### Additional information about the issue\n\n_No response_","author":{"url":"https://github.com/OfekShilon","@type":"Person","name":"OfekShilon"},"datePublished":"2024-10-03T15:49:49Z","url":"https://github.com/microsoft/TypeScript/issues/60130"}
| 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:5af01571-5df2-ed3e-0f49-99a0f518309a |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 80BC:4279A:C7F431:10DD9D7:6B010218 |
| html-safe-nonce | 715e9f2f3853980a244b5e872776981c257b56f70d01dedd114fa97e567b9605 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4MEJDOjQyNzlBOkM3RjQzMToxMEREOUQ3OjZCMDEwMjE4IiwidmlzaXRvcl9pZCI6IjU5NzIyNjkwMDAzMTc1MzQ3NDQiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | c3ae2fbd86014cbcc18e59eb857db7d23fedb302161aa94d78e4e59392176fc9 |
| hovercard-subject-tag | issue:2564397487 |
| 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/60130/issue_layout |
| twitter:image | https://opengraph.githubassets.com/b3ffd8c36773f967d106d5e19cb6e6b646decd8e139d8ae1af726c42e5d59290/microsoft/TypeScript/issues/60130 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/b3ffd8c36773f967d106d5e19cb6e6b646decd8e139d8ae1af726c42e5d59290/microsoft/TypeScript/issues/60130 |
| og:image:alt | 🔎 Search Terms Assertion AND signature 🕗 Version & Regression Information This is the behavior in every version I tried, and I reviewed the FAQ for entries about Assertion Signatures, Generics. ⏯ P... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | OfekShilon |
| hostname | github.com |
| expected-hostname | github.com |
| None | cb7fa109eca7d4156c889f474306b0e91336055e9adf8fd4684b61e49094d124 |
| 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 | 2b59d31a4e44f08b7af67a4b67f8fd0af3befe2a |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width