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

Generic parameters inference in interface #30120

Closed
MasGaNo opened this issue Feb 27, 2019 · 3 comments
Closed

Generic parameters inference in interface #30120

MasGaNo opened this issue Feb 27, 2019 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@MasGaNo
Copy link

MasGaNo commented Feb 27, 2019

TypeScript Version: 3.4.0-dev.20190226

Search Terms:
Generic parameters inference

Code

interface IFoo {
    id: string;
}

interface ITest<T extends IFoo | IFoo[]> {
    data: T;
};

interface ITest2<T extends IFoo | IFoo[]= IFoo> {
    data: T;
}

const test1: ITest = { // Generic type 'ITest<T>' requires 1 type argument(s). ts(2314)
    data: [{
        id: ''
    }]
}

const test2: ITest2 = {
    data: [{ // Property 'id' is missing in type '{ id: string; }[]' but required in type 'IFoo'. ts(2741)
        id: 'fds'
    }]
}

Expected behavior:
I expect the interface ITest to understand that data is typeof Array<IFoo> like for generic method inference.
If I precise a default value for generic parameter (ITest2), I can understand that in my test2 implementation, it will take IFoo as a default implementation, even if I found the error explanation a little bit weird to understand.

Actual behavior:
As describe in the code section, I get some errors because I must provide the expected type.
The thing is, sometime, in some third party library definition, they don't export some type, so I cannot easily pick the expected variant of the generic type I want to have.
A way to fix it, is to recreate a type by mimicking the structure of the original type, but I increase the number of source of truth, so as soon as the third party update the definition, it breaks my duplicated type.

Playground Link:
Playground link

Related Issues:
Well... I found lot of issues but related with generic method (that works like a charm today), not really just for interface...

@RyanCavanaugh
Copy link
Member

Working on this area at #26242

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Feb 27, 2019
@MasGaNo
Copy link
Author

MasGaNo commented Feb 27, 2019

Oh! true, I miss this one!
Great news! I'm looking forward this issue.

@ianstormtaylor
Copy link

Still would really love to see this for defining plain objects that map to a generic type! Would be really helpful for creating inputs that a framework expects without having to use extra factory functions everywhere.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants