Skip to content

Instantly share code, notes, and snippets.

@ksaldana1
Last active June 25, 2018 22:59
Show Gist options
  • Save ksaldana1/e00519aa5ca30e4c26aa68a3c9cde145 to your computer and use it in GitHub Desktop.
Save ksaldana1/e00519aa5ca30e4c26aa68a3c9cde145 to your computer and use it in GitHub Desktop.
#25157 Repro - File 2
import { PathPart, Route } from './interfaces/types';
export function isParam(i: any): i is PathParam<any> {
return i.param != null;
}
export function param<T extends string>(t: T): PathParam<T> {
return { param: t };
}
export interface RouteCreator {
<K extends PathPart<any>>(ks: [K]): Route<K>;
<K extends PathPart<any>, K1 extends PathPart<string>>(ks: [K, K1]): Route<K | K1>;
<K extends PathPart<any>, K1 extends PathPart<any>, K2 extends PathPart<any>>(
ks: [K, K1, K2]
): Route<K | K1 | K2>;
<
K extends PathPart<any>,
K1 extends PathPart<any>,
K2 extends PathPart<any>,
K3 extends PathPart<any>
>(
ks: [K, K1, K2, K3]
): Route<K | K1 | K2 | K3>;
<
K extends PathPart<any>,
K1 extends PathPart<any>,
K2 extends PathPart<any>,
K3 extends PathPart<any>,
K4 extends PathPart<any>
>(
ks: [K, K1, K2, K3, K4]
): Route<K | K1 | K2 | K3 | K4>;
<
K extends PathPart<any>,
K1 extends PathPart<any>,
K2 extends PathPart<any>,
K3 extends PathPart<any>,
K4 extends PathPart<any>,
K5 extends PathPart<any>
>(
ks: [K, K1, K2, K3, K4, K5]
): Route<K | K1 | K2 | K3 | K4 | K5>;
<
K extends PathPart<any>,
K1 extends PathPart<any>,
K2 extends PathPart<any>,
K3 extends PathPart<any>,
K4 extends PathPart<any>,
K5 extends PathPart<any>,
K6 extends PathPart<any>
>(
ks: [K, K1, K2, K3, K4, K5, K6]
): Route<K | K1 | K2 | K3 | K4 | K5 | K6>;
<
K extends PathPart<any>,
K1 extends PathPart<any>,
K2 extends PathPart<any>,
K3 extends PathPart<any>,
K4 extends PathPart<any>,
K5 extends PathPart<any>,
K6 extends PathPart<any>,
K7 extends PathPart<any>
>(
ks: [K, K1, K2, K3, K4, K5, K6, K7]
): Route<K | K1 | K2 | K3 | K4 | K5 | K6 | K7>;
<
K extends PathPart<any>,
K1 extends PathPart<any>,
K2 extends PathPart<any>,
K3 extends PathPart<any>,
K4 extends PathPart<any>,
K5 extends PathPart<any>,
K6 extends PathPart<any>,
K7 extends PathPart<any>
>(
ks: [K, K1, K2, K3, K4, K5, K6, K7]
): Route<K | K1 | K2 | K3 | K4 | K5 | K6 | K7>;
<
K extends PathPart<any>,
K1 extends PathPart<any>,
K2 extends PathPart<any>,
K3 extends PathPart<any>,
K4 extends PathPart<any>,
K5 extends PathPart<any>,
K6 extends PathPart<any>,
K7 extends PathPart<any>,
K8 extends PathPart<any>
>(
ks: [K, K1, K2, K3, K4, K5, K6, K7, K8]
): Route<K | K1 | K2 | K3 | K4 | K5 | K6 | K7 | K8>;
<
K extends PathPart<any>,
K1 extends PathPart<any>,
K2 extends PathPart<any>,
K3 extends PathPart<any>,
K4 extends PathPart<any>,
K5 extends PathPart<any>,
K6 extends PathPart<any>,
K7 extends PathPart<any>,
K8 extends PathPart<any>,
K9 extends PathPart<any>
>(
ks: [K, K1, K2, K3, K4, K5, K6, K7, K8, K9]
): Route<K | K1 | K2 | K3 | K4 | K5 | K6 | K7 | K8 | K9>;
}
export const route: RouteCreator = (pathParts: Array<PathPart<any>>) => {
return {
template: () => {
return (
'/' + pathParts.map(part => (isParam(part) ? `:${part.param}` : part)).join('/')
);
},
create: (params: any) => {
return (
'/' +
pathParts
.map(part => {
if (isParam(part)) {
const { param } = part;
return params[param];
}
return part;
})
.join('/')
);
},
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment