Skip to content

Instantly share code, notes, and snippets.

@chenyong
Last active May 20, 2020 03:38
Show Gist options
  • Save chenyong/8d2386aade2b8327e1c644d348e01f16 to your computer and use it in GitHub Desktop.
Save chenyong/8d2386aade2b8327e1c644d348e01f16 to your computer and use it in GitHub Desktop.
Router code generated.
  let routerTree = parseRoutePath(window.location.hash.slice(1), routerRules); // ->> IRouteParseResult

  ReactDOM.render(<Container router={routerTree as any} />, document.querySelector(".app"));
let Container: FC<{ router: GenRouterTypeMain }> = React.memo((props) => {
ERROR in .../apis/example/main.tsx(29,30):
TS2322: Type 'IRouteParseResult<ISimpleObject, IQueryObject>' is not assignable to type '{ name: "home"; params: {}; query: {}; next: null; } | { name: "home"; params: {}; query: {}; next: null; }'.
  Type 'IRouteParseResult<ISimpleObject, IQueryObject>' is not assignable to type '{ name: "home"; params: {}; query: {}; next: null; }'.
    Types of property 'name' are incompatible.
      Type 'string' is not assignable to type '"home"'.
ℹ 「wdm」: Failed to compile.
import queryString from "query-string";
type Id = string;
function switchPath(x: string) {
location.hash = `#${x}`;
}
function qsStringify(queries: { [k: string]: any }) {
return queryString.stringify(queries, { arrayFormat: "bracket" });
}
// generated
// Generated with router-code-generator@0.2.7
export let genRouter = {
home: {
name: "home",
raw: "home",
path: () => `/home`,
go: () => switchPath(`/home`),
},
$: {
name: "home",
raw: "",
path: () => `/`,
go: () => switchPath(`/`),
},
};
/** Deprecating, use GenRouterTypeTree["next"] instead */
export type GenRouterTypeMain = GenRouterTypeTree["next"];
export interface GenRouterTypeTree {
next: GenRouterTypeTree["home"] | GenRouterTypeTree["$"];
home: {
name: "home";
params: {};
query: {};
next: null;
};
$: {
name: "home";
params: {};
query: {};
next: null;
};
}
import { IRouteRule } from "@jimengio/ruled-router";
export const routerRules: IRouteRule[] = [{ path: "home" }, { path: "", name: "home" }];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment