From f5f8213418a834864f2e09fe2f3bf3a354c9724b Mon Sep 17 00:00:00 2001 From: Peter Wagenet Date: Mon, 24 May 2021 09:46:10 -0700 Subject: [PATCH] Further improve types per feedback --- packages/@ember/-internals/metal/lib/mixin.ts | 2 +- .../routing/lib/location/auto_location.ts | 21 ++++----- .../routing/lib/location/hash_location.ts | 4 +- .../routing/lib/location/history_location.ts | 4 +- .../routing/lib/location/none_location.ts | 6 +-- .../-internals/routing/lib/system/route.ts | 43 +++++++++---------- .../-internals/routing/lib/system/router.ts | 20 ++++----- .../@ember/object/type-tests/compat.test.ts | 2 +- 8 files changed, 50 insertions(+), 52 deletions(-) diff --git a/packages/@ember/-internals/metal/lib/mixin.ts b/packages/@ember/-internals/metal/lib/mixin.ts index 876b7ce997a..42bbf482c32 100644 --- a/packages/@ember/-internals/metal/lib/mixin.ts +++ b/packages/@ember/-internals/metal/lib/mixin.ts @@ -275,7 +275,7 @@ function mergeMixins( } else { mergeProps( meta, - currentMixin as { [key: string]: any }, + currentMixin as Record, descs, values, base, diff --git a/packages/@ember/-internals/routing/lib/location/auto_location.ts b/packages/@ember/-internals/routing/lib/location/auto_location.ts index 2dc61ce7524..7a5fde23700 100644 --- a/packages/@ember/-internals/routing/lib/location/auto_location.ts +++ b/packages/@ember/-internals/routing/lib/location/auto_location.ts @@ -62,10 +62,10 @@ import { @protected */ export default class AutoLocation extends EmberObject implements EmberLocation { - getURL!: () => string; - setURL!: (url: string) => void; - onUpdateURL!: (callback: UpdateCallback) => void; - formatURL!: (url: string) => string; + declare getURL: () => string; + declare setURL: (url: string) => void; + declare onUpdateURL: (callback: UpdateCallback) => void; + declare formatURL: (url: string) => string; concreteImplementation?: EmberLocation; @@ -73,6 +73,7 @@ export default class AutoLocation extends EmberObject implements EmberLocation { // FIXME: This is never set // See https://github.com/emberjs/ember.js/issues/19515 + /** @internal */ documentMode: number | undefined; /** @@ -85,7 +86,7 @@ export default class AutoLocation extends EmberObject implements EmberLocation { @default '/' */ // Added in reopen to allow overriding via extend - rootURL!: string; + declare rootURL: string; /** @private @@ -97,7 +98,7 @@ export default class AutoLocation extends EmberObject implements EmberLocation { @default environment.location */ // Added in reopen to allow overriding via extend - location!: any; + declare location: Location; /** @private @@ -110,7 +111,7 @@ export default class AutoLocation extends EmberObject implements EmberLocation { @default environment.history */ // Added in reopen to allow overriding via extend - history!: any; + declare history: any; /** @private @@ -122,7 +123,7 @@ export default class AutoLocation extends EmberObject implements EmberLocation { @default window */ // Added in reopen to allow overriding via extend - global!: any; + declare global: any; /** @private @@ -135,7 +136,7 @@ export default class AutoLocation extends EmberObject implements EmberLocation { @default environment.history */ // Added in reopen to allow overriding via extend - userAgent!: any; + declare userAgent: string; /** @private @@ -148,7 +149,7 @@ export default class AutoLocation extends EmberObject implements EmberLocation { @default false */ // Added in reopen to allow overriding via extend - cancelRouterSetup!: boolean | undefined; + declare cancelRouterSetup: boolean; /** Called by the router to instruct the location to do any feature detection diff --git a/packages/@ember/-internals/routing/lib/location/hash_location.ts b/packages/@ember/-internals/routing/lib/location/hash_location.ts index 4b8828a6836..afcc655c9c4 100644 --- a/packages/@ember/-internals/routing/lib/location/hash_location.ts +++ b/packages/@ember/-internals/routing/lib/location/hash_location.ts @@ -40,8 +40,8 @@ export default class HashLocation extends EmberObject implements EmberLocation { implementation = 'hash'; _hashchangeHandler?: EventListener; - private _location?: any; - declare location: any; + private _location?: Location; + declare location: Location; init(): void { set(this, 'location', this._location || window.location); diff --git a/packages/@ember/-internals/routing/lib/location/history_location.ts b/packages/@ember/-internals/routing/lib/location/history_location.ts index f79e02a71b8..b2e665d5a07 100644 --- a/packages/@ember/-internals/routing/lib/location/history_location.ts +++ b/packages/@ember/-internals/routing/lib/location/history_location.ts @@ -59,8 +59,8 @@ function _uuid() { @protected */ export default class HistoryLocation extends EmberObject implements EmberLocation { - location!: any; - baseURL!: string; + declare location: Location; + declare baseURL: string; history?: any; diff --git a/packages/@ember/-internals/routing/lib/location/none_location.ts b/packages/@ember/-internals/routing/lib/location/none_location.ts index 435f370c393..a7ea318c471 100644 --- a/packages/@ember/-internals/routing/lib/location/none_location.ts +++ b/packages/@ember/-internals/routing/lib/location/none_location.ts @@ -22,11 +22,11 @@ import { EmberLocation, UpdateCallback } from './api'; @protected */ export default class NoneLocation extends EmberObject implements EmberLocation { - updateCallback!: UpdateCallback; + declare updateCallback: UpdateCallback; implementation = 'none'; // Set in reopen so it can be overwritten with extend - path!: string; + declare path: string; /** Will be pre-pended to path. @@ -36,7 +36,7 @@ export default class NoneLocation extends EmberObject implements EmberLocation { @default '/' */ // Set in reopen so it can be overwritten with extend - rootURL!: string; + declare rootURL: string; detect(): void { let { rootURL } = this; diff --git a/packages/@ember/-internals/routing/lib/system/route.ts b/packages/@ember/-internals/routing/lib/system/route.ts index de4babf3ff3..cfeea8e6fd0 100644 --- a/packages/@ember/-internals/routing/lib/system/route.ts +++ b/packages/@ember/-internals/routing/lib/system/route.ts @@ -108,8 +108,8 @@ class Route extends EmberObject.extend(ActionHandler, Evented) implements IRoute private _names: unknown; _router!: EmberRouter; - _topLevelViewTemplate!: any; - _environment!: any; + declare _topLevelViewTemplate: any; + declare _environment: any; constructor(owner: Owner) { super(...arguments); @@ -131,11 +131,11 @@ class Route extends EmberObject.extend(ActionHandler, Evented) implements IRoute } // Implement Evented - on!: (name: string, method: ((...args: any[]) => void) | string) => this; - one!: (name: string, method: string | ((...args: any[]) => void)) => this; - trigger!: (name: string, ...args: any[]) => any; - off!: (name: string, method: string | ((...args: any[]) => void)) => this; - has!: (name: string) => boolean; + declare on: (name: string, method: ((...args: any[]) => void) | string) => this; + declare one: (name: string, method: string | ((...args: any[]) => void)) => this; + declare trigger: (name: string, ...args: any[]) => any; + declare off: (name: string, method: string | ((...args: any[]) => void)) => this; + declare has: (name: string) => boolean; serialize!: ( model: {}, @@ -190,7 +190,7 @@ class Route extends EmberObject.extend(ActionHandler, Evented) implements IRoute @public */ // Set in reopen so it can be overriden with extend - queryParams!: any; + declare queryParams: Record; /** The name of the template to use by default when rendering this routes @@ -223,7 +223,7 @@ class Route extends EmberObject.extend(ActionHandler, Evented) implements IRoute @public */ // Set in reopen so it can be overriden with extend - templateName!: string | null; + declare templateName: string | null; /** The name of the controller to associate with this route. @@ -246,7 +246,7 @@ class Route extends EmberObject.extend(ActionHandler, Evented) implements IRoute @public */ // Set in reopen so it can be overriden with extend - controllerName!: string | null; + declare controllerName: string | null; /** The controller associated with this route. @@ -277,7 +277,7 @@ class Route extends EmberObject.extend(ActionHandler, Evented) implements IRoute @since 1.6.0 @public */ - controller!: Controller; + declare controller: Controller; /** The name of the route, dot-delimited. @@ -291,7 +291,7 @@ class Route extends EmberObject.extend(ActionHandler, Evented) implements IRoute @since 1.0.0 @public */ - routeName!: string; + declare routeName: string; /** The name of the route, dot-delimited, including the engine prefix @@ -306,7 +306,7 @@ class Route extends EmberObject.extend(ActionHandler, Evented) implements IRoute @since 2.10.0 @public */ - fullRouteName!: string; + declare fullRouteName: string; /** Sets the name for this route, including a fully resolved name for routes @@ -2207,7 +2207,7 @@ class Route extends EmberObject.extend(ActionHandler, Evented) implements IRoute } // Set in reopen - actions!: Record; + declare actions: Record any>; /** Sends an action to the router, which will delegate it to the currently @@ -2258,7 +2258,7 @@ class Route extends EmberObject.extend(ActionHandler, Evented) implements IRoute @public */ // Set with reopen to override parent behavior - send!: (name: string, ...args: any[]) => unknown; + declare send: (name: string, ...args: any[]) => unknown; } function parentRoute(route: Route) { @@ -2271,7 +2271,7 @@ function routeInfoFor(route: Route, routeInfos: InternalRouteInfo[], offs return; } - let current: any; + let current: Route | undefined; for (let i = 0; i < routeInfos.length; i++) { current = routeInfos[i].route; if (current === route) { @@ -2388,17 +2388,14 @@ export interface RenderOptions { into?: string; outlet: string; name: string; - controller: unknown; + controller: Controller | string | undefined; model: unknown; template: Template; } -interface PartialRenderOptions { - into?: string; - outlet?: string; - controller?: string | any; - model?: {}; -} +type PartialRenderOptions = Partial< + Pick +>; export function getFullQueryParams(router: EmberRouter, state: TransitionState) { if (state['fullQueryParams']) { diff --git a/packages/@ember/-internals/routing/lib/system/router.ts b/packages/@ember/-internals/routing/lib/system/router.ts index ea864f7a464..495e1a2f15d 100644 --- a/packages/@ember/-internals/routing/lib/system/router.ts +++ b/packages/@ember/-internals/routing/lib/system/router.ts @@ -152,7 +152,7 @@ class EmberRouter extends EmberObject.extend(Evented) implements Evented { @public */ // Set with reopen to allow overriding via extend - rootURL!: string; + declare rootURL: string; /** The `location` property determines the type of URL's that your @@ -173,7 +173,7 @@ class EmberRouter extends EmberObject.extend(Evented) implements Evented { @public */ // Set with reopen to allow overriding via extend - location!: string | IEmberLocation; + declare location: string | IEmberLocation; _routerMicrolib!: Router; _didSetupRouter = false; @@ -200,11 +200,11 @@ class EmberRouter extends EmberObject.extend(Evented) implements Evented { private namespace: any; // Begin Evented - on!: (name: string, method: ((...args: any[]) => void) | string) => this; - one!: (name: string, method: string | ((...args: any[]) => void)) => this; - trigger!: (name: string, ...args: any[]) => any; - off!: (name: string, method: string | ((...args: any[]) => void)) => this; - has!: (name: string) => boolean; + declare on: (name: string, method: ((...args: any[]) => void) | string) => this; + declare one: (name: string, method: string | ((...args: any[]) => void)) => this; + declare trigger: (name: string, ...args: any[]) => unknown; + declare off: (name: string, method: string | ((...args: any[]) => void)) => this; + declare has: (name: string) => boolean; // End Evented // Set with reopenClass @@ -1408,7 +1408,7 @@ class EmberRouter extends EmberObject.extend(Evented) implements Evented { @since 1.2.0 */ // Set with reopen to allow overriding via extend - didTransition!: typeof defaultDidTransition; + declare didTransition: typeof defaultDidTransition; /** Handles notifying any listeners of an impending URL @@ -1421,7 +1421,7 @@ class EmberRouter extends EmberObject.extend(Evented) implements Evented { @since 1.11.0 */ // Set with reopen to allow overriding via extend - willTransition!: typeof defaultWillTransition; + declare willTransition: typeof defaultWillTransition; /** Represents the current URL. @@ -1431,7 +1431,7 @@ class EmberRouter extends EmberObject.extend(Evented) implements Evented { @private */ // Set with reopen to allow overriding via extend - url!: string; + declare url: string; } /* diff --git a/packages/@ember/object/type-tests/compat.test.ts b/packages/@ember/object/type-tests/compat.test.ts index 840ec5d7fa4..84fa5fc4418 100644 --- a/packages/@ember/object/type-tests/compat.test.ts +++ b/packages/@ember/object/type-tests/compat.test.ts @@ -1,4 +1,4 @@ import { dependentKeyCompat } from '@ember/object/compat'; import { expectTypeOf } from 'expect-type'; -expectTypeOf(dependentKeyCompat).toMatchTypeOf(); +expectTypeOf(dependentKeyCompat).toMatchTypeOf();