Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow augmented declaration merging of type aliases. #28078

Closed
nkappler opened this issue Oct 23, 2018 · 4 comments
Closed

Allow augmented declaration merging of type aliases. #28078

nkappler opened this issue Oct 23, 2018 · 4 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@nkappler
Copy link

nkappler commented Oct 23, 2018

I want to extend a type alias declared in node_modules/@types

File "index.d.ts":

interface A { /* stuff */ }
interface B { /* stuff */ }
interface C { /* stuff */ }

export type X = A | B | C;

My File:

interface D { /* stuff */ }

declare module "someModule" {
  export type X = X | D;
}

This does not work at the moment, and I don't know if there is a good workaround for what I want to achieve, because I want to call a generic function from that module which expects a parameter of type X and I want to pass it a D.

@weswigham weswigham added the Question An issue which isn't directly actionable in code label Oct 23, 2018
@weswigham
Copy link
Member

@nkappler write

interface XRegistry {
  A:A
  B:B
  C:C
}
type X= XRegistry[keyof XRegistry];
//... And in the other file
declare module "someModule" {
  interface XRegistry { D:D }
}

Instead.

@nkappler
Copy link
Author

@weswigham Thanks for that solution, I had that in mind too, however it feels a little clumsy.
Are there any disadvantages or technical issues with allowing the extension of type aliases?

@weswigham
Copy link
Member

I'd hardly call it clumsy, afaik we use it in the Dom lib for some things. But yeah, merging aliases doesn't make nearly as much sense as interfaces. As you've written it, file parse order would matter. And generally speaking, two arbitrary types don't "merge" cleanly. Eg, what would a merge of Qux[T] and Baz extends T ? Qux : never even be?

@shaunluttin
Copy link

shaunluttin commented Nov 24, 2018

How would we make this approach work to extend an external type like this:

someModule.ts

export type Foo = 'bar' | 'baz';

someFile.ts

import { Foo } from 'someModule';

const: Foo = 'qux'; // how could we extend Foo to make this work?

The first commit here is my initial attempt at using the approach @weswigham recommended. The use case is related to this SO question.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

3 participants