René's URL Explorer Experiment


Title: Cascade type narrowing to child constants · Issue #53202 · microsoft/TypeScript · GitHub

Open Graph Title: Cascade type narrowing to child constants · Issue #53202 · microsoft/TypeScript

X Title: Cascade type narrowing to child constants · Issue #53202 · microsoft/TypeScript

Description: Suggestion 🔍 Search Terms narrow child variable related ✅ Viability Checklist My suggestion meets these guidelines: This wouldn't be a breaking change in existing TypeScript/JavaScript code This wouldn't change the runtime behavior of ex...

Open Graph Description: Suggestion 🔍 Search Terms narrow child variable related ✅ Viability Checklist My suggestion meets these guidelines: This wouldn't be a breaking change in existing TypeScript/JavaScript code This wo...

X Description: Suggestion 🔍 Search Terms narrow child variable related ✅ Viability Checklist My suggestion meets these guidelines: This wouldn't be a breaking change in existing TypeScript/JavaScript code Thi...

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

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Cascade type narrowing to child constants","articleBody":"# Suggestion\r\n\r\n\u003c!--\r\n  Please fill in each section completely. Thank you!\r\n-->\r\n\r\n## 🔍 Search Terms\r\n\r\n\u003c!--\r\n  💡 Did you know? TypeScript has over 2,000 open suggestions!\r\n  🔎 Please search thoroughly before logging new feature requests as most common ideas already have a proposal in progress.\r\n  The \"Common Feature Requests\" section of the FAQ lists many popular requests: https://github.com/Microsoft/TypeScript/wiki/FAQ#common-feature-requests\r\n\r\n  Replace the text below:\r\n-->\r\n\r\nnarrow child variable related\r\n\r\n## ✅ Viability Checklist\r\n\r\n\u003c!--\r\n   Suggestions that don't meet all these criteria are very, very unlikely to be accepted.\r\n   We always recommend reviewing the TypeScript design goals before investing time writing\r\n   a proposal for ideas outside the scope of the project.\r\n-->\r\nMy suggestion meets these guidelines:\r\n\r\n* [X] This wouldn't be a breaking change in existing TypeScript/JavaScript code\r\n* [X] This wouldn't change the runtime behavior of existing JavaScript code\r\n* [X] This could be implemented without emitting different JS based on the types of the expressions\r\n* [X] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)\r\n* [X] This feature would agree with the rest of [TypeScript's Design Goals](https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals).\r\n\r\n\r\n## ⭐ Suggestion\r\n\r\nConstants that were assigned a child value of another variable or constant should have their type narrowed alongside their parent.\r\n\r\n## 📃 Motivating Example\r\n\r\nConsider the following code:\r\n```ts\r\ntype SomeUnion = (\r\n  | {type: \"string\", values: readonly string[]}\r\n  | {type: \"number\", values: readonly number[]}\r\n  | {type: \"date\", values: readonly Date[]}\r\n);\r\n\r\nfunction someFunction(union:SomeUnion, index:number): void {\r\n  const value = union.values[index];\r\n  if (union.type === \"string\") {\r\n    union.values; // readonly string[]\r\n    value; // string | number | Date\r\n  }\r\n}\r\n```\r\n\r\nIn `someFunction`, TypeScript keeps the type of `value` as it was originally assigned (`string | number | Date`).\r\n\r\nWhat if dev expects `value` to have been narrowed to `string`?\r\n\r\nThis already happens with the root keys of an object\r\n```ts\r\ntype AnotherUnion = (\r\n    | {type: \"string\", sibling: string, foo: {bar: string}}\r\n    | {type: \"number\", sibling: number, foo: {bar: number}}\r\n    | {type: \"date\", sibling: Date, foo: {bar: Date}}\r\n);\r\n\r\nfunction manualDestructure(union: AnotherUnion): void {\r\n  const {type, sibling, foo: {bar}} = union;\r\n  if (type === \"string\") {\r\n    sibling; // string ✔️ \r\n    bar; // string | number | Date 😿 \r\n  }\r\n}\r\n\r\nfunction argumentDestructure({type, sibling, foo: {bar}}: AnotherUnion): void {\r\n  if (type === \"string\") {\r\n    sibling; // string ✔️ \r\n    bar; // string | number | Date 😿 \r\n  }\r\n}\r\n```\r\n\r\nThis is observable in both examples.\r\n\r\n## 💻 Use Cases\r\n\r\nUse case: Working with discriminated unions holding nested objects and arrays\r\n\r\nTo achieve such narrowing, one currently must adjust coding style. ie:\r\n```ts\r\nfunction adjustedScope(union:SomeUnion, index:number): void {\r\n  if (union.type === \"string\") {\r\n    const value = union.values[index];\r\n    value; // string\r\n  } else if (union.type === \"number\") {\r\n    const value = union.values[index]; // 😭 \r\n    value; // number\r\n  }\r\n}\r\n\r\n// or\r\n\r\nfunction typeAssertion(union:SomeUnion, index:number): void {\r\n  const value = union.values[index];\r\n  if (union.type === \"string\") {\r\n    (value as string); // string\r\n  } else if (union.type === \"number\") {\r\n    (value as number); // number 😭 & risky?\r\n  }\r\n}\r\n```\r\n","author":{"url":"https://github.com/drakuno","@type":"Person","name":"drakuno"},"datePublished":"2023-03-10T22:38:27Z","url":"https://github.com/microsoft/TypeScript/issues/53202"}

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:4451da26-3a77-0e9d-cdec-9a62c6730aac
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idCAA6:157C65:D5A2C2:11DFD2E:6B010263
html-safe-nonce92b216a2ce345ef688cc1572b1dde6d8a7fd20d2225f9fd0ec2d1a84e446562d
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDQUE2OjE1N0M2NTpENUEyQzI6MTFERkQyRTo2QjAxMDI2MyIsInZpc2l0b3JfaWQiOiIzNzU1NDM2Mzc5NDA0ODI5Mjg0IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0=
visitor-hmac9e6af54b97d75214540600e275cd1550017a7eccc52a05c3352682d986202673
hovercard-subject-tagissue:1619710190
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/53202/issue_layout
twitter:imagehttps://opengraph.githubassets.com/cdd3d0b87f3c534ab2a0bdbbc9103d3a379b2630ade16ea41809d50817e3148c/microsoft/TypeScript/issues/53202
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/cdd3d0b87f3c534ab2a0bdbbc9103d3a379b2630ade16ea41809d50817e3148c/microsoft/TypeScript/issues/53202
og:image:altSuggestion 🔍 Search Terms narrow child variable related ✅ Viability Checklist My suggestion meets these guidelines: This wouldn't be a breaking change in existing TypeScript/JavaScript code This wo...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamedrakuno
hostnamegithub.com
expected-hostnamegithub.com
Nonecb7fa109eca7d4156c889f474306b0e91336055e9adf8fd4684b61e49094d124
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
release2b59d31a4e44f08b7af67a4b67f8fd0af3befe2a
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/microsoft/TypeScript/issues/53202#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmicrosoft%2FTypeScript%2Fissues%2F53202
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%2F53202
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/53202
Reloadhttps://github.com/microsoft/TypeScript/issues/53202
Reloadhttps://github.com/microsoft/TypeScript/issues/53202
microsofthttps://github.com/microsoft
TypeScripthttps://github.com/microsoft/TypeScript
Codehttps://github.com/microsoft/TypeScript
Issues5k (5k)https://github.com/microsoft/TypeScript/issues
Pull requests309 (309)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/53202
New issuehttps://github.com/login?return_to=https://github.com/microsoft/TypeScript/issues/53202
Cascade type narrowing to child constantshttps://github.com/microsoft/TypeScript/issues/53202#top
Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featurehttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Awaiting%20More%20Feedback%22
SuggestionAn idea for TypeScripthttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Suggestion%22
https://github.com/drakuno
drakunohttps://github.com/drakuno
on Mar 10, 2023https://github.com/microsoft/TypeScript/issues/53202#issue-1619710190
TypeScript's Design Goalshttps://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
Ryan Cavanaugh (RyanCavanaugh)https://github.com/RyanCavanaugh
SuggestionAn idea for TypeScripthttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Suggestion%22
Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featurehttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Awaiting%20More%20Feedback%22
on Mar 10, 2023https://github.com/microsoft/TypeScript/issues/53202#event-8722680696
Sign up for freehttps://github.com/signup?return_to=https://github.com/microsoft/TypeScript/issues/53202
Sign in to commenthttps://github.com/login?return_to=https://github.com/microsoft/TypeScript/issues/53202
Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featurehttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Awaiting%20More%20Feedback%22
SuggestionAn idea for TypeScripthttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Suggestion%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.