Created
June 25, 2018 22:47
-
-
Save ksaldana1/bbeb7c36110b75852e4e09dc9c1bf5b5 to your computer and use it in GitHub Desktop.
#25157 Repro - File 1
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 returned by our RouteCreator function | |
export interface Route<Parts extends PathPart<any>> { | |
template(): string; | |
create(params: UnionToIntersection<OnlyParams<Parts>>): string; | |
} | |
export interface PathParam<T extends string> { | |
param: T; | |
} | |
// Route consists of tuple of PathParts | |
export type PathPart<T extends string> = string | PathParam<T>; | |
// Pluck only the parameter types from a Route | |
export type OnlyParams<T> = T extends PathParam<infer K> ? Record<K, string> : {}; | |
// Given the parameters of a route I want an object of { paramName: string } | |
// e.g. for const Route = route(['logbook', param('logbookId'), param('otherId')]); | |
// RouteParams<Route> = { logbookId: string, otherId: string } | |
export type RouteParams<T extends Route<any>> = T extends Route<infer X> | |
? UnionToIntersection<OnlyParams<X>> | |
: never; | |
// Type Utils | |
export type GetKeys<U> = U extends Record<infer K, any> ? K : never; | |
export type UnionToIntersection<U extends object> = { | |
[K in GetKeys<U>]: U extends Record<K, infer T> ? T : never | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment