René's URL Explorer Experiment


Title: Suggestion: control flow type analysis could account for reassignments through a closure · Issue #8353 · microsoft/TypeScript · GitHub

Open Graph Title: Suggestion: control flow type analysis could account for reassignments through a closure · Issue #8353 · microsoft/TypeScript

X Title: Suggestion: control flow type analysis could account for reassignments through a closure · Issue #8353 · microsoft/TypeScript

Description: TypeScript Version: 1.9.0-dev.20160427 Code: function iWantString(s: string) { if (typeof s !== "string") throw new TypeError("That is not a string!"); } declare let x: number | string | boolean; function func() { x = 1234; } if (typeof ...

Open Graph Description: TypeScript Version: 1.9.0-dev.20160427 Code: function iWantString(s: string) { if (typeof s !== "string") throw new TypeError("That is not a string!"); } declare let x: number | string | boolean; f...

X Description: TypeScript Version: 1.9.0-dev.20160427 Code: function iWantString(s: string) { if (typeof s !== "string") throw new TypeError("That is not a string!"); } declare let x: number |...

Opengraph URL: https://github.com/microsoft/TypeScript/issues/8353

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Suggestion: control flow type analysis could account for reassignments through a closure","articleBody":"**TypeScript Version:** \n\n1.9.0-dev.20160427\n\n**Code:**\n\n``` ts\nfunction iWantString(s: string) {\n    if (typeof s !== \"string\")\n        throw new TypeError(\"That is not a string!\");\n}\n\ndeclare let x: number | string | boolean;\n\nfunction func() {\n    x = 1234;\n}\n\nif (typeof x === \"string\") {\n    func();\n    iWantString(x);\n}\n```\n\n**Actual behavior:** \nThe call to `func()` doesn't influence the compiler's awareness on the possible types `x` might have after the call. It still thinks it is `string` and the call to `iWantString` fails at runtime.\n\n**Suggestion:**\nThe compiler could account for the possibility that `func` may reassign `x` as a captured variable, by storing a list of all the captured variables `func` may possibly reassign throughout its execution and the types they may hold at all return positions.\n\nFor the above example, the type of `x` would be widened to include the additional types `x` may receive during the execution of `func` (only `number` in this example). Right after the function returns, the type of `x` would be widened to `string | number` and `iWantString(x)` would fail at compile time.\n\n**Examples:**\n\n``` ts\ndeclare let x: boolean | number | string[] | number[];\n\nfunction func() {\n    if (Math.random() > 0.5) {\n        // Since 'x' has not been reassigned in this code path, there's no effect here\n        return;\n    }\n\n    if (Math.random() > 0.5) {\n        x = 4321; // no eventual effect\n        x = true; // 'x' is added to the list with the possible type 'boolean'\n        return;\n    }\n\n    x = 1234; // no eventual effect\n    x = [\"a\", \"b\"]; // no eventual effect\n    x = [1, 2]; // the entry for 'x' is extended to include type 'number[]'\n}\n\nif (typeof x === \"string\") {\n    func();\n    // type of x is widened to 'string | boolean | number[]'\n}\n```\n\nMore complex example that includes further calls within the called function itself to other functions that reassign `x` as a captured variable:\n\n``` ts\ndeclare let x: boolean | number | string[] | number[];\n\nfunction func1() {\n    x = 4321; // no eventual effect\n    x = true; // 'x' is added to the list for 'func1' with the possible type 'boolean'\n}\n\nfunction func2() {\n    x = 1111; // 'x' is added to the list for 'func2' with the possible type 'number'\n}\n\nfunction func3() {\n    if (Math.random() > 0.5) {\n        func1(); // The list generated for 'func2' is merged into the one for 'func3'\n        return;\n    }\n    else {\n        func2(); // no eventual effect\n        x = [2, 3]; // the entry for 'x' is extended to include type 'number[]' \n        return;\n    }\n\n    x = 1234; // no eventual effect\n    x = [\"a\", \"b\"]; // no eventual effect\n    x = [1, 2]; // the entry for 'x' is extended to include type 'number[]'\n}\n\nif (typeof x === \"string\") {\n    func3();\n    // type of 'x' is widened to 'string | boolean | number[]'\n}\n```\n\n**Implementation details:**\n\nThis may look very complicated, but it isn't really compared to what has already been achieved for flow and assignment analysis (which is remarkable!). Here's one way to do it:\n\nFor any given function:\n1. Keep a list of all captured variables that are possibly reassigned in it.\n2. For every code path where one of these variables has been possibly reassigned, accumulate the possible types it may hold right before the function returns. Annotate these types on the list.\n3. A variable that is indirectly reassigned through a call to a secondary function is also considered a reassignment done by the calling function and will be included in the list.\n\nAfter each call to a function:\n1. Lookup the current set of scoped variables (including captured, closure-bound ones).\n2. Query each one of them in the list generated for the function that has just returned and merge the possible set of types specified in the list into the set of currently known ones. \n\nThat's it, nested calls naturally propagate this information backwards as this process is internally applied for them.\n\n(I have considered whether the set specified in list should entirely replace the current one, instead of just being merged. However, there is no guarantee here that in _all_ return positions the function has actually reassigned a particular variable. There's just the _possibility_ it did, unless slightly more advanced analysis is performed to detect when a particular variable is known for certain to have been reassigned in _every_ return path, and then the set can possibly be replaced).\n\n**Limitations:**\n\nFunctions that are called using `.call` or `.apply` or a more complex mechanism, like a `promise`, cannot be easily taken into account. However, I felt that the return for this enhancement is sufficient to justify the reasonable effort needed (on a personal note - I have no capability or time to implement something like this myself - the learning curve itself is very, very steep - but I might have tried if I could).\n\n_Edits `11 May 2016`: improved some phrasing corrected the terminology to 'reassignments` instead of 'mutations', and 'captured variables' instead of 'variables assigned through a closure'._\n","author":{"url":"https://github.com/malibuzios","@type":"Person","name":"malibuzios"},"datePublished":"2016-04-28T15:15:45Z","url":"https://github.com/microsoft/TypeScript/issues/8353"}

react-profiling0
react-import-mapreact,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-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:07e37110-bd18-f6c8-613f-b5c5d99fe3ef
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-id95CE:364B55:8F02FF:BB1D6E:6B00DD6D
html-safe-noncedb66ce9c81ab5fc0088d46ce6dbf42fc5f5a2ca68f93f03692cc6bbcaf21abc2
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5NUNFOjM2NEI1NTo4RjAyRkY6QkIxRDZFOjZCMDBERDZEIiwidmlzaXRvcl9pZCI6IjUzMjgxMTEyMDk3MTc3NTExNDkiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmac00e6f7d32178d357b802ba1d8efb4e32eb9b925a903ce2af2df0f6d6b1f6f2aa
hovercard-subject-tagissue:151669133
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/microsoft/TypeScript/8353/issue_layout
twitter:imagehttps://opengraph.githubassets.com/168c03a2c0c0b3fdf7eeb59c8748e99169c50ebb79773391d16861be90ed09c5/microsoft/TypeScript/issues/8353
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/168c03a2c0c0b3fdf7eeb59c8748e99169c50ebb79773391d16861be90ed09c5/microsoft/TypeScript/issues/8353
og:image:altTypeScript Version: 1.9.0-dev.20160427 Code: function iWantString(s: string) { if (typeof s !== "string") throw new TypeError("That is not a string!"); } declare let x: number | string | boolean; f...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamemalibuzios
hostnamegithub.com
expected-hostnamegithub.com
None043c3f30fbcbe2c6c8e729e10cbbbc90c406546f9fbbb67c648d38f6b7d16daf
turbo-cache-controlno-preview
go-importgithub.com/microsoft/TypeScript git https://github.com/microsoft/TypeScript.git
octolytics-dimension-user_id6154722
octolytics-dimension-user_loginmicrosoft
octolytics-dimension-repository_id20929025
octolytics-dimension-repository_nwomicrosoft/TypeScript
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id20929025
octolytics-dimension-repository_network_root_nwomicrosoft/TypeScript
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release5425dc24e8bd8d6fca9641a12afc984825a43875
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/microsoft/TypeScript/issues/8353#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmicrosoft%2FTypeScript%2Fissues%2F8353
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://github.com/security/advanced-security
Copilot for businessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmicrosoft%2FTypeScript%2Fissues%2F8353
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=microsoft%2FTypeScript
Reloadhttps://github.com/microsoft/TypeScript/issues/8353
Reloadhttps://github.com/microsoft/TypeScript/issues/8353
Reloadhttps://github.com/microsoft/TypeScript/issues/8353
microsofthttps://github.com/microsoft
TypeScripthttps://github.com/microsoft/TypeScript
Codehttps://github.com/microsoft/TypeScript
Issues5k (5k)https://github.com/microsoft/TypeScript/issues
Pull requests308 (308)https://github.com/microsoft/TypeScript/pulls
Actionshttps://github.com/microsoft/TypeScript/actions
Projectshttps://github.com/microsoft/TypeScript/projects
Wikihttps://github.com/microsoft/TypeScript/wiki
Security and qualityhttps://github.com/microsoft/TypeScript/security
Insightshttps://github.com/microsoft/TypeScript/insights
https://github.com/microsoft/TypeScript
New issuehttps://github.com/login?return_to=https://github.com/microsoft/TypeScript/issues/8353
New issuehttps://github.com/login?return_to=https://github.com/microsoft/TypeScript/issues/8353
Suggestion: control flow type analysis could account for reassignments through a closurehttps://github.com/microsoft/TypeScript/issues/8353#top
DeclinedThe issue was declined as something which matches the TypeScript visionhttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Declined%22
SuggestionAn idea for TypeScripthttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Suggestion%22
Too ComplexAn issue which adding support for may be too complex for the value it addshttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Too%20Complex%22
https://github.com/malibuzios
malibuzioshttps://github.com/malibuzios
on Apr 28, 2016https://github.com/microsoft/TypeScript/issues/8353#issue-151669133
https://github.com/mhegazy
Mohamed Hegazy (mhegazy)https://github.com/mhegazy
on Apr 28, 2016https://github.com/microsoft/TypeScript/issues/8353#issuecomment-215492825
https://github.com/Microsoft/TypeScript/blob/master/src/compiler/checker.ts#L2966https://github.com/Microsoft/TypeScript/blob/master/src/compiler/checker.ts#L2966
https://github.com/mhegazy
Mohamed Hegazy (mhegazy)https://github.com/mhegazy
on Apr 28, 2016https://github.com/microsoft/TypeScript/issues/8353#issuecomment-215492939
Anders Hejlsberg (@ahejlsberg)https://github.com/ahejlsberg
https://github.com/malibuzios
malibuzioshttps://github.com/malibuzios
on Apr 28, 2016https://github.com/microsoft/TypeScript/issues/8353#issuecomment-215497446
https://github.com/malibuzios
malibuzioshttps://github.com/malibuzios
on Apr 28, 2016https://github.com/microsoft/TypeScript/issues/8353#issuecomment-215541723
https://github.com/malibuzios
malibuzioshttps://github.com/malibuzios
on Apr 29, 2016https://github.com/microsoft/TypeScript/issues/8353#issuecomment-215636451
https://github.com/zpdDG4gta8XKpMCd
ZpdDG4gta (zpdDG4gta8XKpMCd)https://github.com/zpdDG4gta8XKpMCd
on Apr 29, 2016https://github.com/microsoft/TypeScript/issues/8353#issuecomment-215893498
#7770https://github.com/microsoft/TypeScript/issues/7770
https://github.com/mhegazy
Mohamed Hegazy (mhegazy)https://github.com/mhegazy
on Apr 29, 2016https://github.com/microsoft/TypeScript/issues/8353#issuecomment-215899175
#8381https://github.com/microsoft/TypeScript/issues/8381
#8388https://github.com/microsoft/TypeScript/issues/8388
Mohamed Hegazy (mhegazy)https://github.com/mhegazy
SuggestionAn idea for TypeScripthttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Suggestion%22
Too ComplexAn issue which adding support for may be too complex for the value it addshttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Too%20Complex%22
Won't FixThe severity and priority of this issue do not warrant the time or complexity needed to fix ithttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Won't%20Fix%22
DeclinedThe issue was declined as something which matches the TypeScript visionhttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Declined%22
Won't FixThe severity and priority of this issue do not warrant the time or complexity needed to fix ithttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Won't%20Fix%22
on Apr 29, 2016https://github.com/microsoft/TypeScript/issues/8353#event-646295303
Mohamed Hegazy (mhegazy)https://github.com/mhegazy
completedhttps://github.com/microsoft/TypeScript/issues?q=is%3Aissue%20state%3Aclosed%20archived%3Afalse%20reason%3Acompleted
on Apr 29, 2016https://github.com/microsoft/TypeScript/issues/8353#event-646295719
https://github.com/malibuzios
malibuzioshttps://github.com/malibuzios
on Apr 30, 2016https://github.com/microsoft/TypeScript/issues/8353#issuecomment-215944168
Mohamed Hegazy (@mhegazy)https://github.com/mhegazy
https://github.com/mhegazy
Mohamed Hegazy (mhegazy)https://github.com/mhegazy
on Apr 30, 2016https://github.com/microsoft/TypeScript/issues/8353#issuecomment-215987788
https://github.com/zpdDG4gta8XKpMCd
ZpdDG4gta (zpdDG4gta8XKpMCd)https://github.com/zpdDG4gta8XKpMCd
on Apr 30, 2016https://github.com/microsoft/TypeScript/issues/8353#issuecomment-215988191
malibuzioshttps://github.com/malibuzios
#7770https://github.com/microsoft/TypeScript/issues/7770
https://github.com/malibuzios
malibuzioshttps://github.com/malibuzios
on Apr 30, 2016https://github.com/microsoft/TypeScript/issues/8353#issuecomment-215989064
Mohamed Hegazy (@mhegazy)https://github.com/mhegazy
Aleksey-Bykovhttps://github.com/Aleksey-Bykov
https://github.com/RyanCavanaugh
Ryan Cavanaugh (RyanCavanaugh)https://github.com/RyanCavanaugh
on May 3, 2016https://github.com/microsoft/TypeScript/issues/8353#issuecomment-216592497
malibuzioshttps://github.com/malibuzios
Mohamed Hegazy (@mhegazy)https://github.com/mhegazy
malibuzioshttps://github.com/malibuzios
on May 11, 2016https://github.com/microsoft/TypeScript/issues/8353#event-656768522
https://github.com/malibuzios
malibuzioshttps://github.com/malibuzios
on May 11, 2016https://github.com/microsoft/TypeScript/issues/8353#issuecomment-218384267
microsofthttps://github.com/microsoft
on Jun 19, 2018https://github.com/microsoft/TypeScript/issues/8353#event-1689833954
Sign up for freehttps://github.com/signup?return_to=https://github.com/microsoft/TypeScript/issues/8353
Sign in to commenthttps://github.com/login?return_to=https://github.com/microsoft/TypeScript/issues/8353
DeclinedThe issue was declined as something which matches the TypeScript visionhttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Declined%22
SuggestionAn idea for TypeScripthttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Suggestion%22
Too ComplexAn issue which adding support for may be too complex for the value it addshttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Too%20Complex%22
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.