From eff0aefa398e578abffe91b074f70bbc55535a6d Mon Sep 17 00:00:00 2001 From: Thomas Wang Date: Mon, 9 Nov 2020 17:10:54 -0800 Subject: [PATCH] Improve internal router type --- .../-internals/routing/lib/system/dsl.ts | 23 +++++++++++-------- .../-internals/routing/lib/system/engines.ts | 2 +- .../-internals/routing/lib/system/route.ts | 7 ++++-- 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/packages/@ember/-internals/routing/lib/system/dsl.ts b/packages/@ember/-internals/routing/lib/system/dsl.ts index 10c0e3eb677..91366ef8aef 100644 --- a/packages/@ember/-internals/routing/lib/system/dsl.ts +++ b/packages/@ember/-internals/routing/lib/system/dsl.ts @@ -10,7 +10,7 @@ let uuid = 0; export interface RouteOptions { path?: string; resetNamespace?: boolean; - serialize?: any; + serialize?: (model: {}, params: string[]) => { [key: string]: unknown | undefined }; overrideNameAssertion?: boolean; } @@ -43,16 +43,14 @@ function isOptions(value?: RouteOptions | DSLCallback): value is RouteOptions { } export interface DSLImplOptions { enableLoadingSubstates: boolean; - overrideNameAssertion?: boolean; engineInfo?: EngineInfo; addRouteForEngine(name: string, routeOptions: EngineRouteInfo): void; resolveRouteMap(name: string): Factory; - path?: string; } export default class DSLImpl implements DSL { parent: string | null; - matches: any[]; + matches: Array; enableLoadingSubstates: boolean; explicitIndex = false; options: DSLImplOptions; @@ -69,7 +67,7 @@ export default class DSLImpl implements DSL { route(name: string, callback: DSLCallback): void; route(name: string, options: RouteOptions): void; route(name: string, options: RouteOptions, callback: DSLCallback): void; - route(name: string, _options?: RouteOptions | DSLCallback, _callback?: DSLCallback) { + route(name: string, _options?: RouteOptions | DSLCallback, _callback?: DSLCallback): void { let options: RouteOptions; let callback: Option = null; @@ -127,9 +125,13 @@ export default class DSLImpl implements DSL { createRoute(this, name, options); } } - /* eslint-enable no-dupe-class-members */ - push(url: string, name: string, callback?: MatchCallback, serialize?: any) { + push( + url: string, + name: string, + callback?: MatchCallback, + serialize?: (model: {}, params: string[]) => { [key: string]: unknown | undefined } + ): void { let parts = name.split('.'); if (this.options.engineInfo) { @@ -163,12 +165,15 @@ export default class DSLImpl implements DSL { return (match) => { for (let i = 0; i < dslMatches.length; i += 3) { - match(dslMatches[i]).to(dslMatches[i + 1], dslMatches[i + 2]); + match(dslMatches[i] as string).to( + dslMatches[i + 1] as string, + dslMatches[i + 2] as MatchCallback + ); } }; } - mount(_name: string, options: MountOptions = {}) { + mount(_name: string, options: MountOptions = {}): void { let engineRouteMap = this.options.resolveRouteMap(_name); let name = _name; diff --git a/packages/@ember/-internals/routing/lib/system/engines.ts b/packages/@ember/-internals/routing/lib/system/engines.ts index b6f89fe53ea..4d4ce378f58 100644 --- a/packages/@ember/-internals/routing/lib/system/engines.ts +++ b/packages/@ember/-internals/routing/lib/system/engines.ts @@ -7,5 +7,5 @@ export interface EngineInfo { export interface EngineRouteInfo extends EngineInfo { localFullName: string; - serializeMethod?: any; + serializeMethod?: (model: {}, params: string[]) => { [key: string]: unknown | undefined }; } diff --git a/packages/@ember/-internals/routing/lib/system/route.ts b/packages/@ember/-internals/routing/lib/system/route.ts index 0411715f2b5..3ed36a1101f 100644 --- a/packages/@ember/-internals/routing/lib/system/route.ts +++ b/packages/@ember/-internals/routing/lib/system/route.ts @@ -47,7 +47,10 @@ import EmberRouter, { QueryParam } from './router'; export const ROUTE_CONNECTIONS = new WeakMap(); -export function defaultSerialize(model: {}, params: string[]) { +export function defaultSerialize( + model: {}, + params: string[] +): { [key: string]: unknown } | undefined { if (params.length < 1 || !model) { return; } @@ -67,7 +70,7 @@ export function defaultSerialize(model: {}, params: string[]) { return object; } -export function hasDefaultSerialize(route: Route) { +export function hasDefaultSerialize(route: Route): boolean { return route.serialize === defaultSerialize; }