Title: Extend scope of narrowed consts · Issue #10501 · microsoft/TypeScript · GitHub
Open Graph Title: Extend scope of narrowed consts · Issue #10501 · microsoft/TypeScript
X Title: Extend scope of narrowed consts · Issue #10501 · microsoft/TypeScript
Description: This is a suggestion to lift a limitation of the current flow analysis. Consider this, with strict null checks: function f() { let name: string | null = frob(); if (!name) return; bar(() => name.length); // Error possibly null name } TS ...
Open Graph Description: This is a suggestion to lift a limitation of the current flow analysis. Consider this, with strict null checks: function f() { let name: string | null = frob(); if (!name) return; bar(() => name.le...
X Description: This is a suggestion to lift a limitation of the current flow analysis. Consider this, with strict null checks: function f() { let name: string | null = frob(); if (!name) return; bar(() => name...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/10501
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Extend scope of narrowed consts","articleBody":"This is a suggestion to lift a limitation of the current flow analysis.\n\nConsider this, with strict null checks:\n\n``` ts\nfunction f() {\n let name: string | null = frob();\n if (!name) return;\n bar(() => name.length); // Error possibly null name\n}\n```\n\nTS does not propagate type guards accross function captures.\nRightly so because we don't know when the lambda will actually be called. It can be called much later and at that point `name` might be `null` again. E.g. if right after `bar(...)` we have `name = null`.\n\nNow, consider this variation:\n\n``` ts\nfunction f() {\n const name: string | null = frob();\n if (!name) return;\n bar(() => name.length); // Error possibly null name\n}\n```\n\nI suggest that because `name` is declared with `const`, it can't change. So if at the point of capture it has a _not null_ type guard, then it is correct to propagate this information inside the lambda.\n:point_right: The second example can compile without error.\n","author":{"url":"https://github.com/jods4","@type":"Person","name":"jods4"},"datePublished":"2016-08-23T16:52:30Z","url":"https://github.com/microsoft/TypeScript/issues/10501"}
| 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:2a8dec6e-f679-1e53-53b6-0a3e428de2d9 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8EFC:19B6A2:B5CB60:F06242:6B01C124 |
| html-safe-nonce | cb41fe5f3d4d60f6756aae1a2ccb3eebc793a2b41c74d4d9da7d42294213c51e |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4RUZDOjE5QjZBMjpCNUNCNjA6RjA2MjQyOjZCMDFDMTI0IiwidmlzaXRvcl9pZCI6IjE1MzA4ODQ5NzIxMTY4MjAyNjAiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 2b20518c6b562869e9e6d31b879a8a2de61fbb54d2baaf30a58038d31d208add |
| hovercard-subject-tag | issue:172750940 |
| 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/10501/issue_layout |
| twitter:image | https://opengraph.githubassets.com/b472b98f04596ad499b85101acbdf904c0183c2132f6749de6a9551a7d999dad/microsoft/TypeScript/issues/10501 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/b472b98f04596ad499b85101acbdf904c0183c2132f6749de6a9551a7d999dad/microsoft/TypeScript/issues/10501 |
| og:image:alt | This is a suggestion to lift a limitation of the current flow analysis. Consider this, with strict null checks: function f() { let name: string | null = frob(); if (!name) return; bar(() => name.le... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | jods4 |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| 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 | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width