Title: Narrowing of indexed access type only allows apparent type access; don't allow assignability · Issue #32502 · microsoft/TypeScript · GitHub
Open Graph Title: Narrowing of indexed access type only allows apparent type access; don't allow assignability · Issue #32502 · microsoft/TypeScript
X Title: Narrowing of indexed access type only allows apparent type access; don't allow assignability · Issue #32502 · microsoft/TypeScript
Description: TypeScript Version: 3.6.0-dev.20190720 Search Terms: type narrowing undefined generics early return Code type BaseEvents = {[event: string]: Array
Open Graph Description: TypeScript Version: 3.6.0-dev.20190720 Search Terms: type narrowing undefined generics early return Code type BaseEvents = {[event: string]: Array
X Description: TypeScript Version: 3.6.0-dev.20190720 Search Terms: type narrowing undefined generics early return Code type BaseEvents = {[event: string]: Array<any>}; type Cbs<A extends BaseEvents> ...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/32502
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Narrowing of indexed access type only allows apparent type access; don't allow assignability","articleBody":"\u003c!-- Please try to reproduce the issue with `typescript@next`. It may have already been fixed. -->\r\n**TypeScript Version:** 3.6.0-dev.20190720\r\n\r\n\u003c!-- Search terms you tried before logging this (so others can find this issue more easily) -->\r\n**Search Terms:** type narrowing undefined generics early return\r\n\r\n**Code**\r\n\r\n```ts\r\ntype BaseEvents = {[event: string]: Array\u003cany>};\r\ntype Cbs\u003cA extends BaseEvents> = {\r\n [P in keyof A]: Array\u003c(...args: A[P]) => boolean | Promise\u003cboolean>> | undefined;\r\n}\r\nexport class Events\u003cT extends BaseEvents> {\r\n private cbs: Cbs\u003cT> = {} as any;\r\n async emit\u003cP extends keyof T>(event: P, ...args: T[P]) {\r\n const arr = this.cbs[event]; // typeof arr = ((...args: T[P]) => boolean | Promise\u003cboolean>)[] | undefined\r\n type NonNullArr = NonNullable\u003ctypeof arr>; // ((...args: T[P]) => boolean | Promise\u003cboolean>)[]\r\n if (typeof arr === 'undefined') {\r\n return;\r\n }\r\n // typeof arr should now be ((...args: T[P]) => boolean | Promise\u003cboolean>)[]\r\n const nonNullArr: NonNullArr = arr; // TS2322: Type '((...args: T[P]) => boolean | Promise\u003cboolean>)[] | undefined' is not assignable to type 'NonNullable\u003cCbs\u003cT>[P]>'.\r\n // Type 'undefined' is not assignable to type 'NonNullable\u003cCbs\u003cT>[P]>'.\r\n for (const cb of arr) { // TS2488: Type 'Cbs\u003cT>[P]' must have a '[Symbol.iterator]()' method that returns an iterator.\r\n\r\n }\r\n for (const cb2 of nonNullArr) { // No error\r\n\r\n }\r\n }\r\n}\r\n```\r\n\r\n**Expected behavior:** Compiles without errors.\r\n\r\n**Actual behavior:** Several errors relating to arr not narrowing out the `undefined` portion of the argument:\r\n\r\n```\r\nError:(14, 15) TS2322: Type '((...args: T[P]) => boolean | Promise\u003cboolean>)[] | undefined' is not assignable to type 'NonNullable\u003cCbs\u003cT>[P]>'.\r\n Type 'undefined' is not assignable to type 'NonNullable\u003cCbs\u003cT>[P]>'.\r\nError:(16, 26) TS2488: Type 'Cbs\u003cT>[P]' must have a '[Symbol.iterator]()' method that returns an iterator.\r\n```\r\n\r\n**[Playground Link](http://www.typescriptlang.org/play/#code/C4TwDgpgBAQghgZwgUQG4QHbAVAvFAbwG0J0sAuKBYAJwEsMBzAXUoEEaa4QAeODEAD4AvgG4AUKEhQAwgCMEPNlAgAPYJgAmOeEjSZsgvIXFQzUIgAUoDKAGsIIAPYAzKG1bvO3HgAoAdIFwNIwI7FbMAJR4RnJOTgA2EPxQAD5QljROALZ0SDxxickYgkbpAK4YmhAuDBCaEsLiamBONMBQAMYJiDj6WIoAKirqWjqIKGSGJuZQYPSocBpdCpTyQ0b4BMJQiLsCErOIIBidKrnAPNZqGlU4Ds5ug4K+pAaUlgA0UIH+waGUQYRaIEUyzcydJwYai7TjGYAACzy-k6ChIU2YoigAHpsVApBBXLCaMZfAEgiEwlAgZYojEoIUkil0pkcnkIAV4kySpEiMw0lBKtVahh6mDwfjwNAAHJQ6XlBIJDgk-CyjDyxVwORJHgEonBGiCLG4qBk37-Kk0um4WJc4oC1m5fKM4qCXnMcXguhuXx6twGvC4fAAciFNTqmmDIM9ErMNAgwHKNAwh1jTVjOLxfuJVARTgVmigGCcAHcGdAzRSAdTgfSXcyMlknRz6zy+TGIVCYcX1QqlZxKGqNf2VcTjXjBgBlABMAGZp9PAVKoMHK39KYDazaGXaG472Zyivw3XyBWGRfVgzYcMWOr06IwMFqkvinJLpMGh33nxz1jxnhEgjBv4HYZmB4EQSagzLqGVThqKkbXkWTh3ggCAPk+2rQMAb4EiuX6alhPB-gBtJASBGYuG0pqQtCHSolA+qcCCmbUjOAAsAAcnFLh+JGCBEV7ZOUMIInA6C7CuRCTiA2RxAk-h0BoXA4TQzC+JEQkJnmhaIksUDxomyY4CkSkQCpbQgaBUDprGVEkr4tEwqi06MW4PbDsqLEmrKKicG04jWbZZhNMIQA)**\r\n\r\n**Related Issues:**\r\n\r\n#1726 (closed by design, this is different because we can assume any array is not undefined)\r\n\r\n#31456 (this example has a constraint)","author":{"url":"https://github.com/charles-toller","@type":"Person","name":"charles-toller"},"datePublished":"2019-07-21T20:05:14Z","url":"https://github.com/microsoft/TypeScript/issues/32502"}
| 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:02b98fa3-80ab-47b5-3761-81c2fe1a03fe |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DE70:1EC9BC:CDD430:107560E:6B0102A4 |
| html-safe-nonce | 1ad7c668528f98c70ba4f59d7dd9b4032d0c97daf0b7cdc0c0045e804789c2a5 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJERTcwOjFFQzlCQzpDREQ0MzA6MTA3NTYwRTo2QjAxMDJBNCIsInZpc2l0b3JfaWQiOiI4NDkxNTkzODc0Mzc0OTg0MzU2IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | c834ed60439dc0ad6d4329f33c91770afd69881dda2d8162f7d6ded311ad4f2d |
| hovercard-subject-tag | issue:470819160 |
| 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/32502/issue_layout |
| twitter:image | https://opengraph.githubassets.com/39de452df96b3d5df6dd625f1809a0b982d3492540ded634e0552a82d9e96456/microsoft/TypeScript/issues/32502 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/39de452df96b3d5df6dd625f1809a0b982d3492540ded634e0552a82d9e96456/microsoft/TypeScript/issues/32502 |
| og:image:alt | TypeScript Version: 3.6.0-dev.20190720 Search Terms: type narrowing undefined generics early return Code type BaseEvents = {[event: string]: Array |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | charles-toller |
| 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