Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12111

TypeScript union + generic type

$
0
0

I've got some generic type

export interface Generic<T>{...}

I want to use it as the type for some other generic e.g.

let generics: Array<Generic> = [];

Obviously that doesn't work as I haven't specified its own type. Is there a way for me to create a union type which encapsulates all possible types for T? (So I can then do let generics: Array<Union>)

For example, if my program happens to provide string, number, ClassA and InterfaceB as possibilities for T, this union type would be string | number | ClassA | InterfaceB. But I can't know/declare those upfront (which would defeat the purpose of the generic anyway)

I've tried various approaches but the "compiler" is never happy.


Viewing all articles
Browse latest Browse all 12111

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>