Title: Structural typing undermining type safety · Issue #5933 · microsoft/TypeScript · GitHub
Open Graph Title: Structural typing undermining type safety · Issue #5933 · microsoft/TypeScript
X Title: Structural typing undermining type safety · Issue #5933 · microsoft/TypeScript
Description: class Dog {furColor: string} class Cat {} function walk(d: Dog): void {} function pet(c: Cat): void {} const d: Dog = {furColor: "brown"}; const c: Cat = {}; walk(c); // error, good! pet(d); // no error, bad! Ryan and I have had extensiv...
Open Graph Description: class Dog {furColor: string} class Cat {} function walk(d: Dog): void {} function pet(c: Cat): void {} const d: Dog = {furColor: "brown"}; const c: Cat = {}; walk(c); // error, good! pet(d); // no ...
X Description: class Dog {furColor: string} class Cat {} function walk(d: Dog): void {} function pet(c: Cat): void {} const d: Dog = {furColor: "brown"}; const c: Cat = {}; walk(c); // error, good! pet(...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/5933
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Structural typing undermining type safety","articleBody":"``` javascript\nclass Dog {furColor: string}\nclass Cat {}\n\nfunction walk(d: Dog): void {}\nfunction pet(c: Cat): void {}\n\nconst d: Dog = {furColor: \"brown\"};\nconst c: Cat = {};\n\nwalk(c); // error, good!\npet(d); // no error, bad!\n```\n\nRyan and I have had extensive conversations about this problem. In my opinion this should clearly fail since both Dog and Cat are classes. Type safety should ensure that when we demand a class type the thing passed in should be in the inheritance chain. Structural typing shouldn't apply to classes.\n\nThere's an argument to be made that this case also shouldn't be supported for interfaces, which I'm also sympathetic to. This similar example proves the problem:\n\n``` javascript\ninterface Dog {furColor: string}\ninterface Cat {}\n\nfunction walk(d: Dog): void {}\nfunction pet(c: Cat): void {}\n\nconst d: Dog = {furColor: \"brown\"};\nconst c: Cat = {};\n\nwalk(c); // error, good!\npet(d); // no error, bad!\n```\n\nHowever, if we mark furColor as optional, I think there's a totally valid argument to be made that it should compile and the desired behavior is met, e.g.\n\n``` javascript\ninterface Dog {furColor?: string}\ninterface Cat {}\n\nfunction walk(d: Dog): void {}\nfunction pet(c: Cat): void {}\n\nconst d: Dog = {furColor: \"brown\"};\nconst c: Cat = {};\n\nwalk(c); // no error, good!\npet(d); // no error, good!\n```\n","author":{"url":"https://github.com/alexpomsft","@type":"Person","name":"alexpomsft"},"datePublished":"2015-12-04T16:23:38Z","url":"https://github.com/microsoft/TypeScript/issues/5933"}
| 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:3480a192-e172-954d-4f8f-babbaa69be30 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CAD8:17FB7:5EE3E3:779F74:6B0111F1 |
| html-safe-nonce | 8f80848c639030def11a2a028dac9e20a48bf7ef1028b3df85b24b9739946ea3 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDQUQ4OjE3RkI3OjVFRTNFMzo3NzlGNzQ6NkIwMTExRjEiLCJ2aXNpdG9yX2lkIjoiNzU5Mzg3MTgwMTcxMzc1ODcwNSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 22d6141b1cf9c42b713b17200e2a9042cfa43729f36ad0039d0fc7aea004bfe6 |
| hovercard-subject-tag | issue:120435515 |
| 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/5933/issue_layout |
| twitter:image | https://opengraph.githubassets.com/9ed9bca520cff6b18358f218168fb2b2b4757f98d69c81416f8b42bd3272627d/microsoft/TypeScript/issues/5933 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/9ed9bca520cff6b18358f218168fb2b2b4757f98d69c81416f8b42bd3272627d/microsoft/TypeScript/issues/5933 |
| og:image:alt | class Dog {furColor: string} class Cat {} function walk(d: Dog): void {} function pet(c: Cat): void {} const d: Dog = {furColor: "brown"}; const c: Cat = {}; walk(c); // error, good! pet(d); // no ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | alexpomsft |
| hostname | github.com |
| expected-hostname | github.com |
| None | cb7fa109eca7d4156c889f474306b0e91336055e9adf8fd4684b61e49094d124 |
| 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 | 2b59d31a4e44f08b7af67a4b67f8fd0af3befe2a |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width