René's URL Explorer Experiment


Title: RFC: Support __proto__ literal in object initializers · Issue #38385 · microsoft/TypeScript · GitHub

Open Graph Title: RFC: Support __proto__ literal in object initializers · Issue #38385 · microsoft/TypeScript

X Title: RFC: Support __proto__ literal in object initializers · Issue #38385 · microsoft/TypeScript

Description: This is a new issue to specifically propose and elaborate on a feature I raised in this comment on #30587. Search Terms __proto__ prototype object literal object spread object initializer Suggestion While accessing or mutating an existin...

Open Graph Description: This is a new issue to specifically propose and elaborate on a feature I raised in this comment on #30587. Search Terms __proto__ prototype object literal object spread object initializer Suggestio...

X Description: This is a new issue to specifically propose and elaborate on a feature I raised in this comment on #30587. Search Terms __proto__ prototype object literal object spread object initializer Suggestio...

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

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"RFC: Support __proto__ literal in object initializers","articleBody":"This is a new issue to specifically propose and elaborate on a feature I raised [in this comment](https://github.com/microsoft/TypeScript/issues/30587#issuecomment-614855795) on #30587.\r\n\r\n## Search Terms\r\n\r\n- \\_\\_proto__\r\n- prototype\r\n- object literal\r\n- object spread\r\n- object initializer\r\n\r\n## Suggestion\r\nWhile accessing or mutating an _existing_ object via the `Object.protptype.__proto__` getter/setter is deprecated, to the best of my knowledge defining the prototype of a _new_ object via the object initializer `__proto__` literal is very much encouraged.\r\n\r\nThe rules for specifying the prototype of a new object via these semantics are very well-specified and safe. [See the relevant section of the spec here.](https://www.ecma-international.org/ecma-262/6.0/index.html#sec-__proto__-property-names-in-object-initializers)\r\n\r\n\r\n### Basic support\r\nGiven the following:\r\n```ts\r\nconst foo = {\r\n  __proto__: { a: \"a\" },\r\n  b: \"b\"\r\n};\r\n```\r\n\r\nTypescript currently thinks that `foo` is the following shape:\r\n```ts\r\ntype foo = {\r\n  [\"__proto__\"]: { a: string };\r\n  b: string;\r\n};\r\n```\r\n\r\nwhen in reality, it is:\r\n```ts\r\ntype foo = {\r\n  a: string;\r\n  b: string;\r\n};\r\n```\r\n\r\nTypeScript should be able to correctly detect the type of this object initialization.\r\n\r\n### Strict validity checks\r\nAdditionally, TypeScript should prevent invalid `__proto__` assignments that are \"ignored\" by the spec, and require all values to be `null` or an object. This should fail validation:\r\n\r\n```ts\r\nconst invalid = { __proto__: \"hello\" }\r\n```\r\n\r\n### Correct handling of computed properties\r\n\r\nIt's important to note that per the spec, `__proto__` literals are _not_ the same as regular property assignments.\r\n\r\nThis object initialization, for example:\r\n\r\n```ts\r\nconst foo = {\r\n  __proto__: { a: \"a\" },\r\n  b: \"b\",\r\n  [\"__proto__\"]: { c: \"c\"},\r\n};\r\n```\r\ncreates an object of the following shape:\r\n```ts\r\ntype foo = {\r\n  a: string;\r\n  b: string;\r\n  [\"__proto__\"]: { c: string};\r\n};\r\n```\r\n\r\nGiven this, I would recommend that a `__proto__` literal be forbidden in _type/interface definitions_, such that this is considered a syntax error:\r\n```ts\r\ntype foo = {\r\n  __proto__: { a: string }\r\n}\r\n```\r\nwhile this is an allowable way to specify a **property** named `__proto__` on the type `foo`.\r\n```ts\r\ntype foo = {\r\n  [\"__proto__\"]: string\r\n}\r\n```\r\n\r\n## Use-Cases \u0026 Examples\r\n\r\nThis feature allows TypeScript to correctly understand the shape of objects defined with standard JS semantics. While this pattern isn't especially prevalent, it is an important feature of the language, and should be much more common in one particular use-case where TypeScript currently has a rather severe blind spot:\r\n\r\nTypeScript currently _PREVENTS_ the creation of safe indexed objects derived from existing indexed objects. For example:\r\n\r\nGiven this object, and the goal of \"spreading\" it into a new map:\r\n```ts\r\n// All safe map objects MUST have a `null` prototype.\r\nconst someMapObject: { [key: string]: boolean } = Object.create(null);\r\n```\r\n\r\nThe following is UNSAFE, and probably the most common approach I see people using. TypeScript _should_ catch this, and _should_ issue a compile-time error. See bug #37963. \r\n\r\n```ts\r\nconst unsafeSpreadMapObject: { [key: string]: boolean | undefined } = {\r\n  ...someMapObject,\r\n  foo: false\r\n};\r\n\r\nconsole.log(typeof unsafeSpreadMapObject[\"constructor\"]);\r\n// =\u003e function\r\n\r\nconsole.log(typeof safeSpreadMapObject[\"foo\"]);\r\n// =\u003e boolean\r\n```\r\n\r\nThe following is also UNSAFE. While using `Object.assign` and `Object.create` is a perfectly valid alternative, the `any` returned by `Object.create` propagates through the statement and breaks type safety. (Perhaps the result of `Object.create` should be `unknown` instead of `any`?)\r\n\r\n```ts\r\nconst unsafeAssignMapObject: { [key: string]: boolean | undefined  } = Object.assign(\r\n  Object.create(null),\r\n  { foo: \"this is not boolean\" }\r\n);\r\n\r\nconsole.log(typeof safeSpreadMapObject[\"constructor\"]);\r\n// =\u003e undefined\r\n\r\nconsole.log(typeof safeSpreadMapObject[\"foo\"]);\r\n// =\u003e string\r\n```\r\n\r\nThis is the SAFE way to accomplish this while using object spreads, but TypeScript currently forbids it, since it lacks support for the `__proto__` literal, and incorrectly believes a _property_ of type `null` is being defined:\r\n```ts\r\nconst safeSpreadMapObject: { [key: string]: boolean | undefined } = {\r\n  __proto__: null,\r\n  ...someMapObject,\r\n  foo: false\r\n};\r\n\r\nconsole.log(typeof safeSpreadMapObject[\"constructor\"]);\r\n// =\u003e undefined\r\n\r\nconsole.log(typeof safeSpreadMapObject[\"foo\"]);\r\n// =\u003e boolean\r\n```\r\n\r\n\r\n## Checklist\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, 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## References\r\n- [Object initialization: Prototype mutation (MDN)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer#Prototype_mutation)\r\n- [\\_\\_proto__ Property Names in Object Initializers (ECMAScript Spec)](https://tc39.es/ecma262/#sec-__proto__-property-names-in-object-initializers)","author":{"url":"https://github.com/mike-marcacci","@type":"Person","name":"mike-marcacci"},"datePublished":"2020-05-07T04:43:16.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":17},"url":"https://github.com/38385/TypeScript/issues/38385"}

route-pattern/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:d11f5018-3b31-29a8-d6ec-eb2b5f651a1a
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idAFD4:3F6EBB:17B904:1F902D:6A4D85A6
html-safe-nonce0eef2bb4c544f51a7ca424dc57447a7c2de64c7e7ea0fea18cf26c903a227298
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBRkQ0OjNGNkVCQjoxN0I5MDQ6MUY5MDJEOjZBNEQ4NUE2IiwidmlzaXRvcl9pZCI6IjI4NTU1MjkzNzUyNjM5ODMwMTUiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmac36ed456c4fddde55e4123e4ca957ab810e3339d7e3c362c3da867067a155bc52
hovercard-subject-tagissue:613768265
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/38385/issue_layout
twitter:imagehttps://opengraph.githubassets.com/4a7a35e4463c3d078ef58ec2696167a1409f0d3c27ae6cbdac0c575868d81f2b/microsoft/TypeScript/issues/38385
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/4a7a35e4463c3d078ef58ec2696167a1409f0d3c27ae6cbdac0c575868d81f2b/microsoft/TypeScript/issues/38385
og:image:altThis is a new issue to specifically propose and elaborate on a feature I raised in this comment on #30587. Search Terms __proto__ prototype object literal object spread object initializer Suggestio...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamemike-marcacci
hostnamegithub.com
expected-hostnamegithub.com
None9f8758a3953dfe943439713a6fa4f90d542a3431f10e861ca03dd7f39009f320
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
releasebffd5484f01713a661b03469b77678f72b6574ed
ui-targetcanary-1
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/microsoft/TypeScript/issues/38385#start-of-content
https://github.com/
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmicrosoft%2FTypeScript%2Fissues%2F38385
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub Copilot appDirect agents from issue to mergehttps://github.com/features/ai/github-app
MCP RegistryNewIntegrate external toolshttps://github.com/mcp
ActionsAutomate any workflowhttps://github.com/features/actions
CodespacesInstant dev environmentshttps://github.com/features/codespaces
IssuesPlan and track workhttps://github.com/features/issues
Code ReviewManage code changeshttps://github.com/features/code-review
GitHub Advanced SecurityFind and fix vulnerabilitieshttps://github.com/security/advanced-security
Code securitySecure your code as you buildhttps://github.com/security/advanced-security/code-security
Secret protectionStop leaks before they starthttps://github.com/security/advanced-security/secret-protection
Why GitHubhttps://github.com/why-github
Documentationhttps://docs.github.com
Bloghttps://github.blog
Changeloghttps://github.blog/changelog
Marketplacehttps://github.com/marketplace
View all featureshttps://github.com/features
Enterpriseshttps://github.com/enterprise
Small and medium teamshttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
App Modernizationhttps://github.com/solutions/use-case/app-modernization
DevSecOpshttps://github.com/solutions/use-case/devsecops
DevOpshttps://github.com/solutions/use-case/devops
CI/CDhttps://github.com/solutions/use-case/ci-cd
View all use caseshttps://github.com/solutions/use-case
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
View all industrieshttps://github.com/solutions/industry
View all solutionshttps://github.com/solutions
AIhttps://github.com/resources/articles?topic=ai
Software Developmenthttps://github.com/resources/articles?topic=software-development
DevOpshttps://github.com/resources/articles?topic=devops
Securityhttps://github.com/resources/articles?topic=security
View all topicshttps://github.com/resources/articles
Customer storieshttps://github.com/customer-stories
Events & webinarshttps://github.com/resources/events
Ebooks & reportshttps://github.com/resources/whitepapers
Business insightshttps://github.com/solutions/executive-insights
GitHub Skillshttps://skills.github.com
Documentationhttps://docs.github.com
Customer supporthttps://support.github.com
Community forumhttps://github.com/orgs/community/discussions
Trust centerhttps://github.com/trust-center
Partnershttps://github.com/partners
View all resourceshttps://github.com/resources
GitHub SponsorsFund open source developershttps://github.com/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/accelerator
GitHub Starshttps://stars.github.com
Archive Programhttps://archiveprogram.github.com
Topicshttps://github.com/topics
Trendinghttps://github.com/trending
Collectionshttps://github.com/collections
Enterprise platformAI-powered developer platformhttps://github.com/enterprise
GitHub Advanced SecurityEnterprise-grade security featureshttps://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/premium-support
Pricinghttps://github.com/pricing
Search syntax tipshttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
documentationhttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmicrosoft%2FTypeScript%2Fissues%2F38385
Sign up https://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/38385
Reloadhttps://github.com/microsoft/TypeScript/issues/38385
Reloadhttps://github.com/microsoft/TypeScript/issues/38385
Please reload this pagehttps://github.com/microsoft/TypeScript/issues/38385
microsoft https://github.com/microsoft
TypeScripthttps://github.com/microsoft/TypeScript
Notifications https://github.com/login?return_to=%2Fmicrosoft%2FTypeScript
Fork 13.5k https://github.com/login?return_to=%2Fmicrosoft%2FTypeScript
Star 110k https://github.com/login?return_to=%2Fmicrosoft%2FTypeScript
Code https://github.com/microsoft/TypeScript
Issues 5k+ https://github.com/microsoft/TypeScript/issues
Pull requests 32 https://github.com/microsoft/TypeScript/pulls
Actions https://github.com/microsoft/TypeScript/actions
Projects https://github.com/microsoft/TypeScript/projects
Models https://github.com/microsoft/TypeScript/models
Wiki https://github.com/microsoft/TypeScript/wiki
Security and quality 0 https://github.com/microsoft/TypeScript/security
Insights https://github.com/microsoft/TypeScript/pulse
Code https://github.com/microsoft/TypeScript
Issues https://github.com/microsoft/TypeScript/issues
Pull requests https://github.com/microsoft/TypeScript/pulls
Actions https://github.com/microsoft/TypeScript/actions
Projects https://github.com/microsoft/TypeScript/projects
Models https://github.com/microsoft/TypeScript/models
Wiki https://github.com/microsoft/TypeScript/wiki
Security and quality https://github.com/microsoft/TypeScript/security
Insights https://github.com/microsoft/TypeScript/pulse
RFC: Support __proto__ literal in object initializershttps://github.com/microsoft/TypeScript/issues/38385#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/mike-marcacci
mike-marcaccihttps://github.com/mike-marcacci
on May 7, 2020https://github.com/microsoft/TypeScript/issues/38385#issue-613768265
in this commenthttps://github.com/microsoft/TypeScript/issues/30587#issuecomment-614855795
#30587https://github.com/microsoft/TypeScript/issues/30587
See the relevant section of the spec here.https://www.ecma-international.org/ecma-262/6.0/index.html#sec-__proto__-property-names-in-object-initializers
#37963https://github.com/microsoft/TypeScript/issues/37963
TypeScript's Design Goalshttps://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
Object initialization: Prototype mutation (MDN)https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer#Prototype_mutation
__proto__ Property Names in Object Initializers (ECMAScript Spec)https://tc39.es/ecma262/#sec-__proto__-property-names-in-object-initializers
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.