Title: Object is incorrectly typed as 'possibly null' inside closure · Issue #12113 · microsoft/TypeScript · GitHub
Open Graph Title: Object is incorrectly typed as 'possibly null' inside closure · Issue #12113 · microsoft/TypeScript
X Title: Object is incorrectly typed as 'possibly null' inside closure · Issue #12113 · microsoft/TypeScript
Description: TypeScript Version: 2.0.3 (also the 2.1 RC) Code With --strictNullChecks, class A { constructor() { } method() { } } function f(a: A | null) { a = a || new A(); if (!a) { return; } [1,2,3].forEach((item) => { a.method(); }); } Expected b...
Open Graph Description: TypeScript Version: 2.0.3 (also the 2.1 RC) Code With --strictNullChecks, class A { constructor() { } method() { } } function f(a: A | null) { a = a || new A(); if (!a) { return; } [1,2,3].forEach(...
X Description: TypeScript Version: 2.0.3 (also the 2.1 RC) Code With --strictNullChecks, class A { constructor() { } method() { } } function f(a: A | null) { a = a || new A(); if (!a) { return; } [1,2,3].forEach(...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/12113
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Object is incorrectly typed as 'possibly null' inside closure","articleBody":"\u003c!-- BUGS: Please use this template. -->\r\n\u003c!-- QUESTIONS: This is not a general support forum! Ask Qs at http://stackoverflow.com/questions/tagged/typescript -->\r\n\u003c!-- SUGGESTIONS: See https://github.com/Microsoft/TypeScript-wiki/blob/master/Writing-Good-Design-Proposals.md -->\r\n\r\n**TypeScript Version:** 2.0.3 (also the 2.1 RC)\r\n\r\n**Code**\r\n\r\nWith --strictNullChecks,\r\n\r\n```ts\r\nclass A {\r\n constructor() { }\r\n method() { }\r\n}\r\n\r\nfunction f(a: A | null) {\r\n a = a || new A();\r\n if (!a) {\r\n return;\r\n } \r\n \r\n [1,2,3].forEach((item) => {\r\n a.method();\r\n });\r\n} \r\n\r\n```\r\n\r\n**Expected behavior:**\r\n\r\nI expect this to compile\r\n\r\n**Actual behavior:** \r\n\r\nI get an error,\r\n\r\n`closure.ts(13,5): error TS2531: Object is possibly 'null'.`\r\n\r\nIf I remove the forEach loop and just write `a.method()`, it compiles. Also, if I remove the line `a = a || new A()`, it compiles. Both things together seem to confuse TypeScript.\r\n\r\nIf I remove the `if (!a) { return; }` it fails with the same error, which seems like a bug as well.\r\n\r\nI understand how this closures in general could be difficult to reason about, but in this case, I think TypeScript should be able to figure it out, since the closure function cannot be called until after the ` if (!a) { return; }`, after which point `a` is never assigned.\r\n","author":{"url":"https://github.com/tjhance","@type":"Person","name":"tjhance"},"datePublished":"2016-11-09T00:07:39Z","url":"https://github.com/microsoft/TypeScript/issues/12113"}
| 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:9cfbc2da-5ac5-f87d-7aea-248a910b7b0f |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B262:1DCB8C:E47E8B:12F157C:6B00FED0 |
| html-safe-nonce | d51a0b71df3da47bc68a1eff4ac6b98b2a9c2d4f3302db9cba20343b26bce46d |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCMjYyOjFEQ0I4QzpFNDdFOEI6MTJGMTU3Qzo2QjAwRkVEMCIsInZpc2l0b3JfaWQiOiI4NDMxNjUwNzAzNzQ2ODU4NzA1IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 8dcd1d979bb2207199975d9e13513a2fd119fcc55a9daf6db561e612a420035c |
| hovercard-subject-tag | issue:188138279 |
| 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/12113/issue_layout |
| twitter:image | https://opengraph.githubassets.com/a9dbedc0842b35ac3bd85e25bbcfdbb2879f8a586b3f203d3e92e0618b17f0e9/microsoft/TypeScript/issues/12113 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/a9dbedc0842b35ac3bd85e25bbcfdbb2879f8a586b3f203d3e92e0618b17f0e9/microsoft/TypeScript/issues/12113 |
| og:image:alt | TypeScript Version: 2.0.3 (also the 2.1 RC) Code With --strictNullChecks, class A { constructor() { } method() { } } function f(a: A | null) { a = a || new A(); if (!a) { return; } [1,2,3].forEach(... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | tjhance |
| 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