Title: Support some non-structural (nominal) type matching · Issue #202 · microsoft/TypeScript · GitHub
Open Graph Title: Support some non-structural (nominal) type matching · Issue #202 · microsoft/TypeScript
X Title: Support some non-structural (nominal) type matching · Issue #202 · microsoft/TypeScript
Description: Proposal: support non-structural typing (e.g. new user-defined base-types, or some form of basic nominal typing). This allows programmer to have more refined types supporting frequently used idioms such as: Indexes that come from differe...
Open Graph Description: Proposal: support non-structural typing (e.g. new user-defined base-types, or some form of basic nominal typing). This allows programmer to have more refined types supporting frequently used idioms...
X Description: Proposal: support non-structural typing (e.g. new user-defined base-types, or some form of basic nominal typing). This allows programmer to have more refined types supporting frequently used idioms...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/202
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Support some non-structural (nominal) type matching","articleBody":"Proposal: support non-structural typing (e.g. new user-defined base-types, or some form of basic nominal typing). This allows programmer to have more refined types supporting frequently used idioms such as: \r\n\r\n1) Indexes that come from different tables. Because all indexes are strings (or numbers), it's easy to use the an index variable (intended for one table) with another index variable intended for a different table. Because indexes are the same type, no error is given. If we have abstract index classes this would be fixed.\r\n\r\n2) Certain classes of functions (e.g. callbacks) can be important to be distinguished even though they have the same type. e.g. \"() => void\" often captures a side-effect producing function. Sometimes you want to control which ones are put into an event handler. Currently there's no way to type-check them. \r\n\r\n3) Consider having 2 different interfaces that have different optional parameters but the same required one. In typescript you will not get a compiler error when you provide one but need the other. Sometimes this is ok, but very often this is very not ok and you would love to have a compiler error rather than be confused at run-time.\r\n\r\nProposal (with all type-Error-lines removed!): \r\n\r\n```\r\n// Define FooTable and FooIndex\r\nnominal FooIndex = string; // Proposed new kind of nominal declaration.\r\ninterface FooTable {\r\n [i: FooIndex]: { foo: number };\r\n}\r\nlet s1: FooIndex;\r\nlet t1: FooTable;\r\n\r\n// Define BarTable and BarIndex\r\nnominal BarIndex = string; // Proposed new kind of nominal declaration.\r\ninterface BarTable {\r\n [i: BarIndex]: { bar: string };\r\n}\r\nlet s2: BarIndex;\r\nlet t2: BarTable;\r\n\r\n// For assignment from base-types and basic structures: no type-overloading is needed.\r\ns1 = 'foo1';\r\nt1 = {};\r\nt1[s1] = { foo: 1 };\r\n\r\ns2 = 'bar1';\r\nt2 = { 'bar1': { bar: 'barbar' }};\r\n\r\nconsole.log(s2 = s1); // Proposed to be type error.\r\nconsole.log(s2 == s1); // Proposed to be type error.\r\nconsole.log(s2 === s1); // Proposed to be type error.\r\n\r\nt1[s2].foo = 100; // Gives a runtime error. Proposed to be type error.\r\nt1[s1].foo = 100;\r\n\r\nfunction BadFooTest(t: FooTable) {\r\n if (s2 in t) { // Proposed to be type error.\r\n console.log('cool');\r\n console.log(t[s2].foo); // Proposed to be type error.\r\n }\r\n}\r\n\r\nfunction GoodBarTest(t: BarTable) {\r\n if (s2 in t) {\r\n console.log('cool');\r\n console.log(t[s2].bar);\r\n }\r\n}\r\n\r\nBadFooTest(t1); // Gives runtime error;\r\nBadFooTest(t2); // No runtime error, Proposed to be type error.\r\nGoodBarTest(t1); // Gives runtime error; Proposed to be type error.\r\nGoodBarTest(t2);\r\n```\r\n","author":{"url":"https://github.com/iislucas","@type":"Person","name":"iislucas"},"datePublished":"2014-07-22T23:52:21Z","url":"https://github.com/microsoft/TypeScript/issues/202"}
| 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:8ad30cd7-afcd-aa0e-619b-f68331bf931e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9410:10769A:1CAE63:270D06:6AFBFEEF |
| html-safe-nonce | 5c5f6a0728bfff89ac80a2870139e404aae42163f64302685c1d233f4de3e350 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5NDEwOjEwNzY5QToxQ0FFNjM6MjcwRDA2OjZBRkJGRUVGIiwidmlzaXRvcl9pZCI6Ijc0NzEwODc0OTE0MDM3NDI5NTkiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | f14620f198f92b3fb10be8d552b75a44dddfca9b4330967147b788e0bf7ccaad |
| hovercard-subject-tag | issue:38467435 |
| 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/202/issue_layout |
| twitter:image | https://opengraph.githubassets.com/3368b3f86457cbf1a1aa178c92eb0f4ae32f8cbd27547b9f96b2b59d21468e33/microsoft/TypeScript/issues/202 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/3368b3f86457cbf1a1aa178c92eb0f4ae32f8cbd27547b9f96b2b59d21468e33/microsoft/TypeScript/issues/202 |
| og:image:alt | Proposal: support non-structural typing (e.g. new user-defined base-types, or some form of basic nominal typing). This allows programmer to have more refined types supporting frequently used idioms... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | iislucas |
| hostname | github.com |
| expected-hostname | github.com |
| None | fa74413a1d84dac536fcca0740780bcbd3207bafec0e51873b73ff8e8cca032f |
| 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 | b3b5633a74535d9c23385512d534c66c8fde1907 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width