Created
June 5, 2017 08:04
-
-
Save evansb/7afc5ac7e640a06759276f456970e857 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type DataProvider<TKey extends string, TData> = { | |
getData(): TData | |
} | |
type ProvidedData<K1 extends string, T1> = { [key in K1]: T1 } | |
function getDataFromProviders<K1 extends string, T1>(p1: DataProvider<K1, T1>): ProvidedData<K1, T1> | |
function getDataFromProviders<K1 extends string, T1, K2 extends string, T2>(p1: DataProvider<K1, T1>, p2: DataProvider<K2, T2>): ProvidedData<K1, T1> & ProvidedData<K2, T2> | |
function getDataFromProviders(...providers: DataProvider<any, any>[]): {[name: string]: any } { | |
const result = {}; | |
for (const name of Object.getOwnPropertyNames(providers)) { | |
result[name] = providers[name].getData(); | |
} | |
return result; | |
} | |
const tenProvider : DataProvider<"ten", number> = { | |
getData: () => 10 | |
} | |
const greetProvider : DataProvider<"greet", number> = { | |
getData: () => 'hi' | |
} | |
const values = getDataFromProviders( | |
tenProvider, | |
greetProvider | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment