I have created a type alias that is used on multiple interfaces:
export interface A {}
export interface B {}
export interface C {}
export type Reference = A | B | C;
In my code, I have a method called getReference
which by default returns an array of the Reference type. However, I want this method to be able to take a generic type as input and check if the given type is part of my type alias.
Current implementation:
export const getReference = (slug: ReferencesSlugs): (state: object) => Array<Reference> => {
....... // some code
// return Reference[]
}
Desired implementation:
We want developers to pass a generic type parameter to the method, and TypeScript should verify if the specified type is included in the Reference type alias.
export const getReference = <T>(slug: ReferencesSlugs): (state: object) => Array<T> => {
....... // some code
// If T is within the Reference type -> return an array of T
}
this.store.pipe( select(getReference<A>('onchonch')) ); // This is valid, tslint approves
this.store.pipe( select(getReference<E>('onchonch')) ); // This is invalid, E is not part of the defined type alias.
Thank you in advance :)