Title: Destructured parameter should not enforce presence / non-nullability from type annotation on a property that's default-initialized · Issue #7678 · microsoft/TypeScript · GitHub
Open Graph Title: Destructured parameter should not enforce presence / non-nullability from type annotation on a property that's default-initialized · Issue #7678 · microsoft/TypeScript
X Title: Destructured parameter should not enforce presence / non-nullability from type annotation on a property that's default-initialized · Issue #7678 · microsoft/TypeScript
Description: TypeScript Version: 55cbdc9 (current master) Code type Props = { propA: number, propB: string, propC: number }; var p: Props = { propC: 0 }; // Not a Props, missing propA and propB. Error, as expected var q: Props = { propA: 6, propB: "b...
Open Graph Description: TypeScript Version: 55cbdc9 (current master) Code type Props = { propA: number, propB: string, propC: number }; var p: Props = { propC: 0 }; // Not a Props, missing propA and propB. Error, as expec...
X Description: TypeScript Version: 55cbdc9 (current master) Code type Props = { propA: number, propB: string, propC: number }; var p: Props = { propC: 0 }; // Not a Props, missing propA and propB. Error, as expec...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/7678
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Destructured parameter should not enforce presence / non-nullability from type annotation on a property that's default-initialized","articleBody":"**TypeScript Version:** \n\nhttps://github.com/Microsoft/TypeScript/commit/55cbdc96238c828ed02a54ecd264c7bce0a70acd (current master)\n\n**Code**\n\n``` typescript\ntype Props = { propA: number, propB: string, propC: number };\n\nvar p: Props = { propC: 0 }; // Not a Props, missing propA and propB. Error, as expected\nvar q: Props = { propA: 6, propB: \"b\", propC: 0 }; // Ok, is a Props\nvar r = { propA: 6, propB: \"b\", propC: 0 }; // Not annotated, but is a Props\nvar s = { propA : 6, propC: 0 }; // Not a Props, missing propB. Not annotated, so not an error\nvar t = { propC: \"c\" }; // Not a Props, propC is the wrong type. Not annotated, so not an error\n\n// No type annotation on parameter\nfunction Component({ propA = 5, propB = \"a\", propC }) {\n console.log(propA, propB, propC); // propC is any, undesirable\n}\n\nComponent(q); // No error, expected\nComponent({ propC: 0 }); // No error, expected\nComponent(r); // No error, expected\nComponent(s); // No error, expected\nComponent(t); // Error, expected\n\n// Type annotation on parameter\nfunction Component2({ propA = 5, propB = \"a\", propC }: Props) {\n console.log(propA, propB, propC); // propC is number, desirable\n}\n\nComponent2(q); // No error, expected\nComponent2({ propC: 0 }); // Error, propA and propB are missing\nComponent2(r); // No error, expected\nComponent2(s); // Error, propB is missing\nComponent2(t); // Error, expected\n```\n\n```\ntsc -t es5 ./foo.ts\n```\n\n**Expected behavior:**\n\n`Component2({ propC: 0 })` and `Component2(s)` compile even though their parameters don't have propA and propB from `Props`, because `Component2` default-initializes those parameters.\n\n**Actual behavior:** \n\n```\nfoo.ts(26,12): error TS2345: Argument of type '{ propC: number; }' is not assignable to parameter of type '{ propA: number; propB: string; propC: number; }'.\n Property 'propA' is missing in type '{ propC: number; }'.\nfoo.ts(28,12): error TS2345: Argument of type '{ propA: number; propC: number; }' is not assignable to parameter of type '{ propA: number; propB: string; propC: number; }'.\n Property 'propB' is missing in type '{ propA: number; propC: number; }'.\n```\n\nplus errors about `p` and `Component2(t)` which are expected.\n\n---\n\nThe reason to add the annotation on `Component2` is to get type information for the non-default-initialized property `propC`.\n\nIs there any downside to allowing `Component2({ propC: 0 })` and `Component2(s)` ? `Component2(t)` should of course still not be allowed. `s` and `{ propC: 0 }` should still not be considered to be `Props` for the purpose of type inference.\n\nOtherwise the user has to make a new type for each such function based on what's default-initialized and what isn't, like `type PropsForComponentFunction = { propA?: number, propB?: string, propC: number }`, and make sure the types match with the original `Props`. (Or inline the whole type declaration into `Component2` but that has the same drawback.)\n","author":{"url":"https://github.com/Arnavion","@type":"Person","name":"Arnavion"},"datePublished":"2016-03-24T22:19:45Z","url":"https://github.com/microsoft/TypeScript/issues/7678"}
| 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:653dbe35-1ae7-f530-7ad5-b139415068f8 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8410:269AD9:35DF5D:49F4B3:6B00C739 |
| html-safe-nonce | 98ccb03d432d73180b817783d13511888d911137849d48c9f330d35d07a6ec77 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4NDEwOjI2OUFEOTozNURGNUQ6NDlGNEIzOjZCMDBDNzM5IiwidmlzaXRvcl9pZCI6IjgxNzA2MzcyNjQ3NjY5NDUwODIiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 94ed5b2d47d481e1520227f399b3535a7ebeea656f8a1bc3c02818fff89c9bad |
| hovercard-subject-tag | issue:143375303 |
| 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/7678/issue_layout |
| twitter:image | https://opengraph.githubassets.com/b2ed4ef14cab1a5901880f1596252f6acc1585f5129f116369094bbfbef1eaf4/microsoft/TypeScript/issues/7678 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/b2ed4ef14cab1a5901880f1596252f6acc1585f5129f116369094bbfbef1eaf4/microsoft/TypeScript/issues/7678 |
| og:image:alt | TypeScript Version: 55cbdc9 (current master) Code type Props = { propA: number, propB: string, propC: number }; var p: Props = { propC: 0 }; // Not a Props, missing propA and propB. Error, as expec... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Arnavion |
| hostname | github.com |
| expected-hostname | github.com |
| None | 98dc00e171c204462a6f367482a0f8c3c6b5dbca99cd7390e82d2df49ce096e4 |
| 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 | ef013399cf0a3c324bea644a685d7c1e0f06b5b7 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width