core-api: fix type inference of createRouteRef

This commit is contained in:
Patrik Oldsberg
2021-02-05 17:49:41 +01:00
parent 0e640893df
commit 2ab3b3f0e3
4 changed files with 22 additions and 14 deletions
+19 -4
View File
@@ -14,7 +14,16 @@
* limitations under the License.
*/
import { RouteRefConfig, RouteRef } from './types';
import { RouteRef } from './types';
import { IconComponent } from '../icons';
export type RouteRefConfig<Params extends { [param in string]: string }> = {
params?: Array<keyof Params>;
/** @deprecated Route refs no longer decide their own path */
path?: string;
icon?: IconComponent;
title: string;
};
export class AbsoluteRouteRef<Params extends { [param in string]: string }> {
constructor(private readonly config: RouteRefConfig<Params>) {}
@@ -38,9 +47,15 @@ export class AbsoluteRouteRef<Params extends { [param in string]: string }> {
}
export function createRouteRef<
ParamKeys extends string,
Params extends { [param in string]: string } = { [name in ParamKeys]: string }
>(config: RouteRefConfig<Params>): RouteRef<Params> {
Params extends { [param in ParamKey]: string },
ParamKey extends string = never
>(config: {
params?: ParamKey[];
/** @deprecated Route refs no longer decide their own path */
path?: string;
icon?: IconComponent;
title: string;
}): RouteRef<Params> {
return new AbsoluteRouteRef<Params>(config);
}
+2 -1
View File
@@ -39,8 +39,9 @@ import {
createRouteRef,
createExternalRouteRef,
ExternalRouteRef,
RouteRefConfig,
} from './RouteRef';
import { RouteRef, RouteRefConfig } from './types';
import { RouteRef } from './types';
const mockConfig = (extra?: Partial<RouteRefConfig<{}>>) => ({
path: '/unused',
+1 -1
View File
@@ -16,11 +16,11 @@
export type {
RouteRef,
RouteRefConfig,
AbsoluteRouteRef,
ConcreteRoute,
MutableRouteRef,
} from './types';
export { FlatRoutes } from './FlatRoutes';
export { createRouteRef } from './RouteRef';
export type { RouteRefConfig } from './RouteRef';
export { useRouteRef } from './hooks';
-8
View File
@@ -45,14 +45,6 @@ export type AbsoluteRouteRef = RouteRef<{}>;
*/
export type MutableRouteRef = RouteRef<{}>;
export type RouteRefConfig<Params extends { [param in string]: string }> = {
params?: Array<keyof Params>;
/** @deprecated Route refs no longer decide their own path */
path?: string;
icon?: IconComponent;
title: string;
};
// A duplicate of the react-router RouteObject, but with routeRef added
export interface BackstageRouteObject {
caseSensitive: boolean;