Created
August 22, 2019 18:17
-
-
Save iansan5653/7430e04d534798f31161aa8bcf70141b to your computer and use it in GitHub Desktop.
types
This file contains hidden or 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
interface ExampleQuery { | |
name?: string; | |
age?: number; | |
child?: { | |
id?: number; | |
name?: string; | |
}; | |
} | |
const GET = Symbol(); | |
/** Recursively map `T` such that all primitive properties become of type GET or their original type. */ | |
type QueryForm<T> = { | |
[K in keyof T]: (T[K] extends object ? (QueryForm<T[K]>) : (T[K] | typeof GET)) | |
}; | |
/** Recursively filter `T` by the keys of `S`. */ | |
type DeepFilterBy<T, S> = { [K in (keyof S & keyof T)]: (T[K] extends object ? (DeepFilterBy<T[K], S[K]>) : (S[K] extends typeof GET ? T[K] : S[K])) }; | |
/** | |
* Submit a query based on the given interface, returning the desired fields. | |
* @param get Object matching the interface shape but where the requested fields | |
* are given the value `true`. | |
*/ | |
function query<S extends QueryForm<ExampleQuery>>(get?: S): DeepFilterBy<ExampleQuery, S> { | |
return; | |
} | |
const queryResponse = query({ | |
name: GET, | |
child: { | |
id: 234, | |
name: GET | |
} | |
}); | |
queryResponse.name | |
queryResponse.child.id | |
queryResponse.age |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment