Last active
January 20, 2021 13:38
-
-
Save KEIII/eaa62c063ad9d488ca8c910f19216161 to your computer and use it in GitHub Desktop.
Type safe build URL ( https://dev.to/0916dhkim/type-safe-usage-of-react-router-5c44 )
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
| type ParamValue = string | number; | |
| type ExtractRouteParams<T> = string extends T | |
| ? Record<ParamValue, ParamValue> | |
| : T extends `${infer _Start}:${infer Param}/${infer Rest}` | |
| ? { [k in Param | keyof ExtractRouteParams<Rest>]: ParamValue } | |
| : T extends `${infer _Start}:${infer Param}` | |
| ? { [k in Param]: ParamValue } | |
| : {}; | |
| export const buildUrl = <T extends string>( | |
| path: T, | |
| params: ExtractRouteParams<T> | |
| ): string => { | |
| return Object.entries(params).reduce<string>((result, [key, value]) => { | |
| return result.replace(`:${key}`, String(value)); | |
| }, path); | |
| }; | |
| buildUrl("/product/:id/:page", { id: 42, page: 2 }); // OK | |
| buildUrl("/product/:id/:page", { id: 42 }); // ERR: Argument of type '{ id: number; }' is not assignable to parameter of type '{ id: ParamValue; page: ParamValue; }' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment