Here is a scenario where I need to consolidate and refactor two types:
...
.then((result1: any) => {
let promises = {
one: $q.when(val1),
two: $q.when(val2)
};
return $q.all(promises);
})
.then((resolvedPromises: any) => {
// use
resolvedPromises.one...
resolvedPromises.two
});
I want to define a single type (possibly generic) for both promises
and resolvedPromises
, instead of having separate types with similar definitions.
Instead of:
public interface ResultingType {
[id: string]: any,
one: Something1,
two: Something1
}
public interface PromisingType {
[id: string]: ng.IPromise<any>,
one: ng.IPromise<Something1>,
two: ng.IPromise<Something2>
}
The challenge lies in defining the outer type, either as ng.IPromise<>
or nothing.
I want to avoid creating a type that allows mixing both types:
public interface ConsolidatedType {
[id: string]: any | ng.IPromise<any>,
one: Something1 | ng.IPromise<Something1>,
two: Something2 | ng.IPromise<Something2>
}
This would lead to inconsistencies. Are there any other efficient ways to create a single type without redundant code that only wraps types into promises?