Skip to content

Generic type parameters constraints are not constraining nested occurences #21452

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

Closed
sztrzask opened this issue Jan 29, 2018 · 2 comments
Closed
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@sztrzask
Copy link

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

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Jan 29, 2018
@RyanCavanaugh
Copy link
Member

There's nothing wrong with that call. B is a valid A<IA>, and IA is a valid IA.

@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@microsoft microsoft locked and limited conversation to collaborators Jul 3, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

3 participants