Skip to content

Generic type parameters constraints are not constraining nested occurences #21452

Closed
@sztrzask

Description

@sztrzask

TypeScript Version: 2.6.2

Search Terms:
Generic, parameter, constraint
Code

//Given abstract generic class

interface IA { 
    id: string;
}

abstract class A<TA extends IA> { 
    abstract save(): TA
}

//Given that's class concerete implementation
interface IB extends IA { 
    name: string;
}

class B extends A<IB>
{ 
    save(): IB { 
        return null as any as IB;
    }
}


//How to enforce the "matching" types?
class C { 

    callSave<TClass extends A<TInterface>,
        TInterface extends IA>(): TInterface { 
        return null as any as TInterface;
        }
}

var c = new C();

//In C# that would fail
c.callSave<B, IA>();

Expected behavior:

Code does not compile

Actual behavior:

Code compiles

Playground Link:
link
Related Issues:
#21249

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions