Skip to content

Smart structural generic type inference support #23733

Closed
@pmunin

Description

@pmunin

TypeScript Version: 2.8.1

Problem: when there are too many generic parameters (e.g. nested generic interfaces), it gets hard to remember the order of those parameters. Some smart structural generic type inference needed.

Code

type ComplexGeneric1<TArg1, TArg2>={
    arg1:TArg1,
    arg2:TArg2,
}
type ComplexGeneric2<TArg1, TArg2>={
    arg1:TArg1,
    arg2:TArg2,
}

interface IMyInterface<TArg11=number,TArg12=number, TArg21=number, TArg22=number>
{
    myFunc(p1:ComplexGeneric1<TArg11, TArg12>, p2:ComplexGeneric2<TArg21, TArg22>);
}

var actualImplementation:IMyInterface<number,string,number, string>={
    myFunc(p1,p2)
    {
        //
    }
}

There should be a way to declare IMyInterface generic to be able to create implementation the following way:

var desiredImplementation:IMyInterface<{ 
        p1:{arg1:number, arg2:string},
        p2:{arg1:number, arg2:string}
    }>=
{
    myFunc(p1,p2)
    {
        //p1.arg1 = 123
        //p1.arg2 = "hello world"
    }
}

Is there a way to define IMyInterface to make it happen? Am I the only one who would like to have it?

I would expect something like this to work, but it doesn't:

interface IMyInterface<TStructure extends ({p1: {arg1:TArg11, arg2:TArg12},p2:{arg1:TArg21, arg2:TArg22}}), TArg11=number,TArg12=number, TArg21=number, TArg22=number>
{
    myFunc(p1:ComplexGeneric1<TArg11, TArg12>, p2:ComplexGeneric2<TArg21, TArg22>);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions