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

Utility type to extract `Foo` from `type Bar = Set<Foo>` with just `Bar`

I am looking for a utility type that will allow me to extract the generic parameter of a set, but I only have available the typed set, so basically

type Bar = Set<Foo>;
type Baz = SomethingMagic<Bar>; // Baz should equal Foo

>Solution :

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

You can do that with the infer keyword:

type SomethingMagic<T> = T extends Set<infer U> ? U : never

Playground link

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