Title: Suggestion: Allow local types to be declared in interfaces · Issue #9889 · microsoft/TypeScript · GitHub
Open Graph Title: Suggestion: Allow local types to be declared in interfaces · Issue #9889 · microsoft/TypeScript
X Title: Suggestion: Allow local types to be declared in interfaces · Issue #9889 · microsoft/TypeScript
Description: I have something like this: export interface Reducer Open Graph Description: I have something like this: export interface Reducer X Description: I have something like this: export interface Reducer<State, Types extends Action<string, any>> { add<TypeName extends string, Payload>(action: { type: TypeName, reduce: (state: St...
Mail addresses
Opengraph URL: https://github.com/microsoft/TypeScript/issues/9889
X: @github
Domain: github.com
Links:
Viewport: width=device-width
notifications@github.com
Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Suggestion: Allow local types to be declared in interfaces","articleBody":"I have something like this:\n\n```\nexport interface Reducer\u003cState, Types extends Action\u003cstring, any>> {\n\n add\u003cTypeName extends string, Payload>(action: {\n type: TypeName,\n reduce: (state: State, action: Payload) => State\n }): Reducer\u003cState, Types | Action\u003cTypeName, Payload>>;\n\n readonly cursorType: Cursor\u003cState, Types>;\n}\n```\n\nThe details aren't that important except to illustrate that a `Reducer` is immutable, and has an `add` method that returns another `Reducer`, but see that the return type has something extra \"unioned\" into it. By repeated chained calls to `add` I can build up a big nasty old type that would be ugly to have fully declare by hand. Fortunately type inference takes care of building the type for me, which is great.\n\nThen elsewhere in my code I want to be able to declare something called a \"cursor\", which needs to have a type that corresponds to the reducer's type. The cursor could be a field in a class so I need to be able to refer to the type so I can declare such a field.\n\nSo I want to provide a simple way to declare a const of the type \"correct kind of cursor for a given reducer\", leveraging the work that the TS compiler already did for me with its type inference.\n\nMy slightly hacky approach, as shown above, is to declare a readonly field `cursorType`. The value of this is at runtime is junk and should not be used! So I need a \"here be dragons\" comment on it. Its only purpose is to be prefixed with `typeof`, e.g.:\n\n```\nconst R = getReducerSomehow();\n\nclass Test {\n constructor(public readonly myCursor: typeof R.cursorType) { }\n}\n```\n\nTo fill in the `cursorType` field of a `Reducer` I have to do this filth:\n\n```\nnewReducer.cursorType = {} as Cursor\u003cState, Types>;\n```\n\nSo `cursorType` really should never be used as a value. It doesn't even need to exist as a value. It will cause a runtime error if anyone tries to used it as a cursor. Ugh. But how else can I make this elaborately computed type available conveniently?\n\nI'm wondering if TS could allow:\n\n```\nexport interface Reducer\u003cState, Types extends Action\u003cstring, any>> {\n\n add\u003cTypeName extends string, Payload>(action: {\n type: TypeName,\n reduce: (state: State, action: Payload) => State\n }): Reducer\u003cState, Types | Action\u003cTypeName, Payload>>;\n\n // not currently possible:\n type CursorType = Cursor\u003cState, Types>;\n}\n```\n\ni.e. a `type` alias can be added to an interface. So now my implementation of `Reducer` no longer has to do anything. No nasty dummy runtime variable hack required.\n\nAnd my usage example becomes:\n\n```\nconst R = getReducerSomehow();\n\nclass Test {\n constructor(public readonly myCursor: R.CursorType) { }\n}\n```\n\nThat is, `CursorType` is a type that can be referred to _as if_ it was a member of an instance. Similar I guess to:\n\n```\nnamespace N {\n export type S = string;\n}\n\nconst s: N.S = \"hi\";\n```\n\nIn which `N` is an object at runtime and yet can also be used to find the type `S`.\n","author":{"url":"https://github.com/danielearwicker","@type":"Person","name":"danielearwicker"},"datePublished":"2016-07-22T12:57:44Z","url":"https://github.com/microsoft/TypeScript/issues/9889"}
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:64178213-f091-a5ed-c69c-94219756cff8 current-catalog-service-hash 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 request-id 87BA:4E3C:D003A7:114CA12:6B00F958 html-safe-nonce a1a287a71d6721032063c9cf62b63a55a1412987d96830cc5527073243a3a9b5 visitor-payload eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4N0JBOjRFM0M6RDAwM0E3OjExNENBMTI6NkIwMEY5NTgiLCJ2aXNpdG9yX2lkIjoiODM0OTM2NDg1MTI1MTkzNTU3NiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 visitor-hmac 704d1218f0670b85cf375d7603e2f8fd7aba4bdb68f03ba0c1cd4fbf9850586e hovercard-subject-tag issue:167039715 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/9889/issue_layout twitter:image https://opengraph.githubassets.com/daf7b6cd680b3ea02e941dbe104f4fb7df623a45283b846e3ca136892bbe67ed/microsoft/TypeScript/issues/9889 twitter:card summary_large_image og:image https://opengraph.githubassets.com/daf7b6cd680b3ea02e941dbe104f4fb7df623a45283b846e3ca136892bbe67ed/microsoft/TypeScript/issues/9889 og:image:alt I have something like this: export interface Reducer og:image:width 1200 og:image:height 600 og:site_name GitHub og:type object og:author:username danielearwicker hostname github.com expected-hostname github.com None e208d86428ec00a57ade75058166297440e8128a01e5287c31a53dc042bbf187 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 7dafd017fb7809a839c443dba08622af38e63c09 ui-target full theme-color #1e2327 color-scheme light dark react-app-name issues
URLs of crawlers that visited me.