Skip to content

Commit

Permalink
fix(deps): bring F.Narrow from ts-toolbelt to internal code
Browse files Browse the repository at this point in the history
  • Loading branch information
fredericoo committed Nov 18, 2023
1 parent 18e560e commit 9ffe212
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
Binary file modified bun.lockb
Binary file not shown.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
"prettier": "^3.0.0",
"react": "^18.2.0",
"react-router-dom": "^6.16.0",
"ts-toolbelt": "^9.6.0",
"tsup": "^7.1.0",
"typescript": "^5.1.6"
},
Expand Down
14 changes: 12 additions & 2 deletions src/browser-router.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import { RouteObject, createBrowserRouter } from 'react-router-dom';
import { F } from 'ts-toolbelt';

/** No deps implementation of F.Narrow from 'ts-toolbelt' */
type Try<A1 extends any, A2 extends any, Catch = never> = A1 extends A2 ? A1 : Catch;
type Narrowable = string | number | bigint | boolean;
type NarrowRaw<A> =
| (A extends [] ? [] : never)
| (A extends Narrowable ? A : never)
| {
[K in keyof A]: A[K] extends Function ? A[K] : NarrowRaw<A[K]>;
};
type Narrow<A extends any> = Try<A, [], NarrowRaw<A>>;

type Flatten<T> = { [K in keyof T]: T[K] } & {};

Expand All @@ -23,7 +33,7 @@ const joinValidWith =
(...valid: any[]) =>
valid.filter(Boolean).join(separator);

export const typesafeBrowserRouter = <R extends RouteObject>(routes: F.Narrow<R[]>) => {
export const typesafeBrowserRouter = <R extends RouteObject>(routes: Narrow<R[]>) => {
function href<P extends ExtractPaths<R>>(
params: { path: Extract<P, string> } & PathParams<Flatten<ExtractParams<P>>> & RouteExtraParams,
) {
Expand Down

0 comments on commit 9ffe212

Please sign in to comment.