Title: Allow to directly reference the types associated with interface properties · Issue #4555 · microsoft/TypeScript · GitHub
Open Graph Title: Allow to directly reference the types associated with interface properties · Issue #4555 · microsoft/TypeScript
X Title: Allow to directly reference the types associated with interface properties · Issue #4555 · microsoft/TypeScript
Description: interface A { prop: { x: number; y: number; } } Trying to associate the type of prop with a variable would lead to an error: let p: A.prop; // Error: cannot find namespace A Aside from anonymous object interfaces, it could also be allowe...
Open Graph Description: interface A { prop: { x: number; y: number; } } Trying to associate the type of prop with a variable would lead to an error: let p: A.prop; // Error: cannot find namespace A Aside from anonymous ob...
X Description: interface A { prop: { x: number; y: number; } } Trying to associate the type of prop with a variable would lead to an error: let p: A.prop; // Error: cannot find namespace A Aside from anonymous ob...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/4555
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Allow to directly reference the types associated with interface properties","articleBody":"``` ts\ninterface A {\n prop: {\n x: number;\n y: number;\n }\n}\n```\n\nTrying to associate the type of `prop` with a variable would lead to an error:\n\n``` ts\nlet p: A.prop; // Error: cannot find namespace A\n```\n\nAside from anonymous object interfaces, it could also be allowed for function types:\n\n``` ts\ninterface A {\n func(arg: number): string\n}\n\nlet f: A.func; // type of f is now func(arg: number): string\nf = (arg: number) => \"hello\"; // OK\n```\n\nPrimitive types:\n\n``` ts\ninterface A {\n prop: boolean;\n}\n\nlet val: A.prop; // type of val is now boolean\nval = true; // OK\n```\n\nAnd also when the types are nested:\n\n``` ts\ninterface A {\n outerProp: {\n innerProp: {\n x: number;\n y: number;\n }\n }\n}\n\nlet p: A.outerProp.innerProp; // type of p would now be {x: number, y:number}\np = { x: 42, y: 24 } //OK\n```\n\nThe current workaround (the best that could I find, at least [edit: I added an additional one that uses `typeof` in the next comment]) is to define the type of `prop` as a named interface or alternatively use the `type` keyword.\n\n``` ts\ninterface PropInterface {\n x: number;\n y: number;\n}\n// Or, alternatively:\ntype PropInterface = { x: number, y: number };\n\ninterface A {\n prop: PropInterface;\n}\n\nlet p: PropInterface; // OK\n```\n\nBut that isn't always necessary or elegant, especially if the types are nested or generic (short example below).\n\nThis can also be extended to types defined through the `type` keyword or when using `typeof SomeClass`. Or even with generics:\n\n``` ts\ninterface A\u003cT> { \n prop: { func: (val: T) => T } \n}\n\nlet f: A\u003cnumber>.prop.func // working around this may be possible, but wouldn't be pretty.\n```\n\nA real-world use case I encountered was casting a value into the type of an interface member from a union that includes it (this happened when thinking about issues with [assignability of merged/intersected types](https://github.com/Microsoft/TypeScript/issues/4302#issuecomment-136277111)):\n\n``` ts\nlet merged: A merge B; // Also happens with intersection: A & B\nlet obj: A;\nobj.prop = \u003cA.prop> merged.prop; // the type of merged.prop is the union A.prop | B.prop\n\n// Edit: It turns out that a relatively simple workaround is possible with the 'typeof' operator, \n// that is explained in the next comment:\nobj.pro = \u003ctypeof obj.prop> merged.prop;\n```\n\n@RyanCavanaugh Too Complex? Perhaps at least keep it as an option for the future?\n","author":{"url":"https://github.com/rotemdan","@type":"Person","name":"rotemdan"},"datePublished":"2015-08-31T10:51:29Z","url":"https://github.com/microsoft/TypeScript/issues/4555"}
| 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:8ae6d3a6-32ba-00eb-c247-c103adc2817d |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | AC14:6DC60:63C214:7D4D07:6B0112AF |
| html-safe-nonce | cdcc6e6ee3fcd25fc3bede2b354433387c347a3ca431b74c45db0888fbfa0360 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBQzE0OjZEQzYwOjYzQzIxNDo3RDREMDc6NkIwMTEyQUYiLCJ2aXNpdG9yX2lkIjoiNDIwNjkzODQ1OTg0NTIzNTM3NSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | a8134861a128c2b1c1b9679296c72785336ef9fda6077e4956c2ddf1d5c68aad |
| hovercard-subject-tag | issue:104030035 |
| 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/4555/issue_layout |
| twitter:image | https://opengraph.githubassets.com/3c39ca82f024182fad071cc2646034cdd0ee4616d6ddb138e1f6f38fd312f592/microsoft/TypeScript/issues/4555 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/3c39ca82f024182fad071cc2646034cdd0ee4616d6ddb138e1f6f38fd312f592/microsoft/TypeScript/issues/4555 |
| og:image:alt | interface A { prop: { x: number; y: number; } } Trying to associate the type of prop with a variable would lead to an error: let p: A.prop; // Error: cannot find namespace A Aside from anonymous ob... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | rotemdan |
| 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