Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Type attribute with Condition in TypeScript

Currently I pass a Type Attribute to a class.

public class MyClass<Type> {...}

But now I want the condition to exist that the given Type implements an interface.
This is my vain attempt to solve this problem:

public class MyClass<Type implements MyInterface> {...}

Maybe this helps to understand what I want to achieve.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Thank you in advance!

>Solution :

In TypeScript these are called generic constraints.

This is how you would write it in your case:

public class MyClass<Type extends MyInterface> {...}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading