Title: 'Discarding Parameters' plus 'Duck Typing' makes every parameter optional? · Issue #9352 · microsoft/TypeScript · GitHub
Open Graph Title: 'Discarding Parameters' plus 'Duck Typing' makes every parameter optional? · Issue #9352 · microsoft/TypeScript
X Title: 'Discarding Parameters' plus 'Duck Typing' makes every parameter optional? · Issue #9352 · microsoft/TypeScript
Description: After reading the Comparing two functions section of the handbook I got myself pretty confused and concerned. I have an example below that I think (taken to the prepare-for-duck-typing-extreme) means I should code every class's method as...
Open Graph Description: After reading the Comparing two functions section of the handbook I got myself pretty confused and concerned. I have an example below that I think (taken to the prepare-for-duck-typing-extreme) mea...
X Description: After reading the Comparing two functions section of the handbook I got myself pretty confused and concerned. I have an example below that I think (taken to the prepare-for-duck-typing-extreme) mea...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/9352
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"'Discarding Parameters' plus 'Duck Typing' makes every parameter optional?","articleBody":"After reading the [Comparing two functions](https://github.com/Microsoft/TypeScript-Handbook/blob/master/pages/Type%20Compatibility.md#comparing-two-functions) section of the handbook I got myself pretty confused and concerned. I have an example below that I think (taken to the prepare-for-duck-typing-extreme) means I should code every class's method as if all of its parameters are optional (even though they're not defined as such).\n\n**TypeScript Version:** \n\n1.8.10 / next (1.9.0-dev.20160624-1.0)\n\n**Code**\n\n``` ts\n(function () { \"use strict\";\n\n\nclass C1 { one(...hi: string[]) { } }\nclass C2 { one(hi: string) { hi.length; } }\n\nfunction Test() {\n const c2 = new C2();\n const c2as1: C1 = c2; // BAD assignment\n\n // TypeError: Cannot read property 'length' of undefined\n c2as1.one(); // BAD execution\n\n // error TS2346: Supplied parameters do not match any signature of call target.\n //c2.one();\n\n // error TS2345: Argument of type 'undefined' is not assignable to parameter of type 'string'.\n // Happens once you enable TypeScript 2.0's --strictNullChecks.\n //c2as1.one(void 0);\n //c2.one(void 0);\n}\n\n\n})();\n```\n\n**Actual behavior:** \n\nI have to remember to write **run-time** checking of `typeof hi === \"string\" ? hi.length : 0` instead of the simple/easy `hi.length` inside my `C2#one(hi: string)` method to prevent against a TypeError.\n\n**Expected behavior:**\n\nI'd love for there to be some way for TypeScript to stop the `BAD assignment` line above; maybe one that turns off 'discarding' parameters altogether?\n\nThe defense given for discarding parameters is a signature like `Array#forEach` -- where callers commonly omit the 2nd/3rd parameters to the `callbackfn`. That doesn't make sense to me though, why isn't the \u003ckbd>*.d.ts\u003c/kbd> for `Array#forEach` just defined as:\n\n``` ts\nforEach(callbackfn: (value?: T, index?: number, array?: T[]) => void, thisArg?: any): void;\n```\n\nInstead of how it is?\n\n``` ts\nforEach(callbackfn: (value: T, index: number, array: T[]) => void, thisArg?: any): void;\n```\n\nMaybe my targeting of parameter discarding is too strong, but it _really_ seems like there should be some way to configure TypeScript to block the [assertion](https://github.com/Microsoft/TypeScript-Handbook/blob/master/pages/Basic%20Types.md#type-assertions) of a `C2` instance to a `C1` interface without requiring me to just write better code like:\n\n``` ts\nclass betterC1 { one(hi: string, ...restHi: string[]) { } }\n```\n","author":{"url":"https://github.com/SlurpTheo","@type":"Person","name":"SlurpTheo"},"datePublished":"2016-06-24T22:01:49Z","url":"https://github.com/microsoft/TypeScript/issues/9352"}
| 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:ae735059-4212-f1ad-7024-7178c4ba706b |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E9CC:272DC0:55A710:6ECB4A:6B00DAB8 |
| html-safe-nonce | d75ad14eff5ec981d4346d1558184b49c119b0bb8a6f5353230c14d856c71fb7 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFOUNDOjI3MkRDMDo1NUE3MTA6NkVDQjRBOjZCMDBEQUI4IiwidmlzaXRvcl9pZCI6Ijg0NjAxNjUzMDk0NTI1NzMzNjgiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | e701d6c44f34f605c12588604abbf6f0d431ecaea2c4025c937490c674b05a7a |
| hovercard-subject-tag | issue:162238912 |
| 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/9352/issue_layout |
| twitter:image | https://opengraph.githubassets.com/e1b74c297e6a1943f1a5900d0f1a007950248ef09740ac34547142453822ef8a/microsoft/TypeScript/issues/9352 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/e1b74c297e6a1943f1a5900d0f1a007950248ef09740ac34547142453822ef8a/microsoft/TypeScript/issues/9352 |
| og:image:alt | After reading the Comparing two functions section of the handbook I got myself pretty confused and concerned. I have an example below that I think (taken to the prepare-for-duck-typing-extreme) mea... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | SlurpTheo |
| hostname | github.com |
| expected-hostname | github.com |
| None | a481f62a1e04c9b9bdcb5628230682c66d59006f5b13b080f79cba84a176905b |
| 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 | 84c8c5cc25e2fb7f2308428458538436baecc6d2 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width