Title: Less restrictive object type · Issue #18228 · microsoft/TypeScript · GitHub
Open Graph Title: Less restrictive object type · Issue #18228 · microsoft/TypeScript
X Title: Less restrictive object type · Issue #18228 · microsoft/TypeScript
Description: New behavior No property existence check for object type. let foo: object; foo.bar; // no error Rationale The current object type is useful when used to type function arguments but not very useful on object members. See this IDL: [Secure...
Open Graph Description: New behavior No property existence check for object type. let foo: object; foo.bar; // no error Rationale The current object type is useful when used to type function arguments but not very useful ...
X Description: New behavior No property existence check for object type. let foo: object; foo.bar; // no error Rationale The current object type is useful when used to type function arguments but not very useful ...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/18228
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Less restrictive object type","articleBody":"### New behavior\r\n\r\nNo property existence check for `object` type.\r\n\r\n```ts\r\nlet foo: object;\r\nfoo.bar; // no error\r\n```\r\n\r\n### Rationale\r\n\r\nThe current `object` type is useful when used to type function arguments but not very useful on object members. See this IDL:\r\n\r\n```webidl\r\n[SecureContext]\r\ninterface PaymentResponse {\r\n [Default] object toJSON();\r\n\r\n readonly attribute DOMString requestId;\r\n readonly attribute DOMString methodName;\r\n readonly attribute object details;\r\n readonly attribute PaymentAddress? shippingAddress;\r\n readonly attribute DOMString? shippingOption;\r\n readonly attribute DOMString? payerName;\r\n readonly attribute DOMString? payerEmail;\r\n readonly attribute DOMString? payerPhone;\r\n\r\n Promise\u003cvoid> complete(optional PaymentComplete result = \"unknown\");\r\n};\r\n```\r\n\r\nHere, the `details` member can be any \"object\" depending on payment method behavior. Current lib.d.ts defines this member as `any` so that a user can access any untyped members.\r\n\r\n```ts\r\nlet response: PaymentResponse;\r\nlet { details } = response;\r\ndetails.anImplementationDependentMemberName; // great\r\n```\r\n\r\nBut `any` means it can be `boolean`, `number`, `string` or even `null` or `undefined`.\r\n\r\n```ts\r\nif (typeof details === \"number\") {\r\n // `details` is resolved as `number` but really should be `never` here\r\n}\r\ndetails++; // no error, :(\r\n```\r\n\r\nA language service should be able to warn on these cases while still allowing arbitrary member accesses. Allowing such free accesses on `object` type will help here:\r\n\r\n```ts\r\ndetails.anImplementationDependentMemberName; // still great\r\nif (typeof details === \"number\") {\r\n // it cannot be a number, so `never` here\r\n}\r\ndetails++; // error now :D\r\n```\r\n\r\n### Hey, a `function` can also be an `object` in TS!\r\n\r\nYes, but I don't think the fact will cause any problem as functions can also have arbitrary members. And I'm not proposing arbitrary function call.","author":{"url":"https://github.com/saschanaz","@type":"Person","name":"saschanaz"},"datePublished":"2017-09-03T12:50:01Z","url":"https://github.com/microsoft/TypeScript/issues/18228"}
| 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:ed4eb609-6988-43f0-03bc-91a8c3928f94 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E91E:1201C:A1B6D0:D8FFB2:6B00EA8C |
| html-safe-nonce | db7b451428878f4b4e6622323e6f87adeb76abdbc8429237e156529e0daa851c |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFOTFFOjEyMDFDOkExQjZEMDpEOEZGQjI6NkIwMEVBOEMiLCJ2aXNpdG9yX2lkIjoiMTI5NjQxMjU1NDEyNTQzNzU4MCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 58833507197a46440c18edad7fb462ee1ed0ad53af4795203791de4978a72ec4 |
| hovercard-subject-tag | issue:254872616 |
| 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/18228/issue_layout |
| twitter:image | https://opengraph.githubassets.com/4c984b7a79d9536fd15f543bd66309285148080308022224b717550e22446354/microsoft/TypeScript/issues/18228 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/4c984b7a79d9536fd15f543bd66309285148080308022224b717550e22446354/microsoft/TypeScript/issues/18228 |
| og:image:alt | New behavior No property existence check for object type. let foo: object; foo.bar; // no error Rationale The current object type is useful when used to type function arguments but not very useful ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | saschanaz |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4ef498cbabcbe3c50befc7a5b743e7072ee770b4daf0e15e54801180d1fa91fc |
| 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 | 9983dd5da06943f833c971dd5b6bc988e2858be5 |
| ui-target | canary-2 |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width