René's URL Explorer Experiment


Title: Proposal: optionally declared variables · Issue #23602 · microsoft/TypeScript · GitHub

Open Graph Title: Proposal: optionally declared variables · Issue #23602 · microsoft/TypeScript

X Title: Proposal: optionally declared variables · Issue #23602 · microsoft/TypeScript

Description: Scenario When writing code that will run in both Node.js and browser environments (e.g. a React component compatible with SSR), it's sometimes necessary that part of the code only executes in one environment and not the other. A few brow...

Open Graph Description: Scenario When writing code that will run in both Node.js and browser environments (e.g. a React component compatible with SSR), it's sometimes necessary that part of the code only executes in one e...

X Description: Scenario When writing code that will run in both Node.js and browser environments (e.g. a React component compatible with SSR), it's sometimes necessary that part of the code only executes in o...

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

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Proposal: optionally declared variables","articleBody":"# Scenario\r\n\r\nWhen writing code that will run in both Node.js and browser environments (e.g. a React component compatible with SSR), it's sometimes necessary that part of the code only executes in one environment and not the other.\r\n\r\nA few browser-only examples:\r\n\r\n- `document.querySelector()` to find an element in the DOM.\r\n- `document.title = \"new title\"` to update the page title.\r\n\r\nTo avoid executing these in Node.js, the following pattern is used:\r\n\r\n```ts\r\nif (typeof document !== \"undefined\") {\r\n  // Safely use `document`.\r\n  document.title = \"new title\";\r\n}\r\n```\r\n\r\nNow the developer needs to configure TypeScript to include the types for `document`. It basically boils down to them deciding whether or not to include `dom` in the `lib` compiler option. \r\n\r\n(`dom.d.ts` is distributed with TypeScript and includes:)\r\n\r\n```ts\r\ndeclare var document: Document;\r\n```\r\n\r\nUnfortunately both options have compromises:\r\n\r\n- **don't include it** and `document` won't be declared, and TypeScript will warn `Cannot find name 'document'.`\r\n- **do include it** and `document` will be declared as a `Document`, but not as `Document | undefined`, so [`strict-type-predicates`](https://palantir.github.io/tslint/rules/strict-type-predicates/) will complain that the check is unnecessary\r\n\r\nA motivated developer may choose to fork `dom.d.ts` and add `| undefined` to all of the global variable declarations, and satisfy `strict-type-predicates`.\r\n\r\nHowever one final piece of safety is still missing: ensuring `typeof document !== \"undefined\"` is used, rather than `document !== undefined`. At runtime if `document` (or any name) isn't declared, and is used outside `typeof`, a `ReferenceError` is thrown. For this reason it's necessary to always choose `typeof` when performing environment \"sniffing\".\r\n\r\nTypeScript currently cannot model \"optionally declared variables\", where a variable \"might\" be declared. Without this, TypeScript doesn't have enough information to warn about code that _may_ throw `ReferenceError`.\r\n\r\n# Proposal\r\n\r\nIntroduce new syntax and type system concepts to allow the \"optional declaration\" of variables. Emit would be unaffected.\r\n\r\n**Proposed syntax:**\r\n\r\n```ts\r\ndeclare? var window: Window;\r\n```\r\n\r\nThis is distinct from:\r\n\r\n```ts\r\ndeclare var window: Window | undefined;\r\n```\r\n\r\nIn the first case, it's describing that `window` may not be declared, and referencing `window` may throw a `ReferenceError`. In the second case, `window` is *always* declared, but might have the value `undefined`.\r\n\r\n**Implementation:**\r\n\r\n- The grammar would need to be extended to match `?` following `declare`.\r\n- The AST would need a representation (e.g. a flag for `DeclareKeyword` or a new `OptionalDeclareKeyword`).\r\n- The type system would need to track if a variable is optionally-declared, and support type-narrowing to a standard variable via `typeof` guards.\r\n- For this to actually be useful, the type checker should warn if \"optionally declared\" variables are referenced. \r\n- If a variable is \"optionally declared\", that information should be included in the string representation (e.g. `declare? var document: Document`), e.g. when hovering a name in VS Code.\r\n\r\n**Shortcomings:**\r\n\r\n- It would be convenient if a single `typeof window !== \"undefined\"` guard could safely grant access to other browser globals (e.g. `document`, `performance`, etc). This current proposal would require separate `typeof` checks for each global.","author":{"url":"https://github.com/bradleyayers","@type":"Person","name":"bradleyayers"},"datePublished":"2018-04-21T07:55:12Z","url":"https://github.com/microsoft/TypeScript/issues/23602"}

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:6fb3886c-6f57-ec10-89ca-191402583b47
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idB320:2D2F2F:3084F99:3DE7B52:6B01BCC4
html-safe-nonce2501bf19027b4870f361511ef62ec163b3100086b523999e4c6783407576c846
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCMzIwOjJEMkYyRjozMDg0Rjk5OjNERTdCNTI6NkIwMUJDQzQiLCJ2aXNpdG9yX2lkIjoiMjY1NTI5Njk0NzY2OTI4NjA4NCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9
visitor-hmac70fa6004c704bd3ce9f458cbb3ecf31560f4e45d116fcc8f466b13d6f0a01341
hovercard-subject-tagissue:316471560
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/23602/issue_layout
twitter:imagehttps://opengraph.githubassets.com/20a96d00fe8f23a3bcbdd8e0bf40d3af4ff6145138eec9685ec2e27113830aba/microsoft/TypeScript/issues/23602
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/20a96d00fe8f23a3bcbdd8e0bf40d3af4ff6145138eec9685ec2e27113830aba/microsoft/TypeScript/issues/23602
og:image:altScenario When writing code that will run in both Node.js and browser environments (e.g. a React component compatible with SSR), it's sometimes necessary that part of the code only executes in one e...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamebradleyayers
hostnamegithub.com
expected-hostnamegithub.com
Nonecc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c
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
release8c77c9e8439031ad54c75a250694842892f693e1
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/microsoft/TypeScript/issues/23602#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmicrosoft%2FTypeScript%2Fissues%2F23602
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%2F23602
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/23602
Reloadhttps://github.com/microsoft/TypeScript/issues/23602
Reloadhttps://github.com/microsoft/TypeScript/issues/23602
microsofthttps://github.com/microsoft
TypeScripthttps://github.com/microsoft/TypeScript
Codehttps://github.com/microsoft/TypeScript
Issues5k (5k)https://github.com/microsoft/TypeScript/issues
Pull requests312 (312)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/23602
New issuehttps://github.com/login?return_to=https://github.com/microsoft/TypeScript/issues/23602
Proposal: optionally declared variableshttps://github.com/microsoft/TypeScript/issues/23602#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/bradleyayers
Bradley Ayers (bradleyayers)https://github.com/bradleyayers
on Apr 21, 2018https://github.com/microsoft/TypeScript/issues/23602#issue-316471560
strict-type-predicateshttps://palantir.github.io/tslint/rules/strict-type-predicates/
https://github.com/mhegazy
Mohamed Hegazy (mhegazy)https://github.com/mhegazy
on Apr 21, 2018https://github.com/microsoft/TypeScript/issues/23602#issuecomment-383323762
Mohamed Hegazy (mhegazy)https://github.com/mhegazy
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 Apr 21, 2018https://github.com/microsoft/TypeScript/issues/23602#event-1587581447
https://github.com/estaub
Ed Staub (estaub)https://github.com/estaub
on Apr 22, 2018https://github.com/microsoft/TypeScript/issues/23602#issuecomment-383354776
Mohamed Hegazy (@mhegazy)https://github.com/mhegazy
https://github.com/Microsoft/TypeScript/issueshttps://github.com/Microsoft/TypeScript/issues
https://github.com/bradleyayers
Bradley Ayers (bradleyayers)https://github.com/bradleyayers
on Apr 23, 2018https://github.com/microsoft/TypeScript/issues/23602#issuecomment-383423565
https://github.com/estaub
Ed Staub (estaub)https://github.com/estaub
on Apr 23, 2018https://github.com/microsoft/TypeScript/issues/23602#issuecomment-383425808
https://github.com/mhegazy
Mohamed Hegazy (mhegazy)https://github.com/mhegazy
on Apr 23, 2018https://github.com/microsoft/TypeScript/issues/23602#issuecomment-383645652
#13195https://github.com/microsoft/TypeScript/issues/13195
https://github.com/ganeshkbhat
Krishnamurthy G B (ganeshkbhat)https://github.com/ganeshkbhat
on Jul 11, 2018https://github.com/microsoft/TypeScript/issues/23602#issuecomment-404048528
#25570https://github.com/microsoft/TypeScript/issues/25570
https://github.com/bradleyayers
Bradley Ayers (bradleyayers)https://github.com/bradleyayers
on Feb 3, 2019https://github.com/microsoft/TypeScript/issues/23602#issuecomment-460046186
#29332https://github.com/microsoft/TypeScript/pull/29332
Bradley Ayers (bradleyayers)https://github.com/bradleyayers
completedhttps://github.com/microsoft/TypeScript/issues?q=is%3Aissue%20state%3Aclosed%20archived%3Afalse%20reason%3Acompleted
on Feb 3, 2019https://github.com/microsoft/TypeScript/issues/23602#event-2114275687
Sign up for freehttps://github.com/signup?return_to=https://github.com/microsoft/TypeScript/issues/23602
Sign in to commenthttps://github.com/login?return_to=https://github.com/microsoft/TypeScript/issues/23602
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.