diff --git a/packages/router/src/interfaces.ts b/packages/router/src/interfaces.ts index 35314168a2..460c5468a8 100644 --- a/packages/router/src/interfaces.ts +++ b/packages/router/src/interfaces.ts @@ -374,6 +374,7 @@ export interface Resolve { * * @publicApi */ -export interface CanLoad { - canLoad(route: Route, segments: UrlSegment[]): Observable|Promise|boolean; -} +export interface CanLoad { canLoad: CanLoadFn; } + +export type CanLoadFn = (route: Route, segments: UrlSegment[]) => + Observable| Promise| boolean; diff --git a/packages/router/src/utils/type_guards.ts b/packages/router/src/utils/type_guards.ts new file mode 100644 index 0000000000..83656af52c --- /dev/null +++ b/packages/router/src/utils/type_guards.ts @@ -0,0 +1,39 @@ +/** + * @license + * Copyright Google Inc. All Rights Reserved. + * + * Use of this source code is governed by an MIT-style license that can be + * found in the LICENSE file at https://angular.io/license + */ + +import {Type} from '@angular/core'; +import {CanActivate, CanActivateChild, CanDeactivate} from '../interfaces'; + +/** + * Simple function check, but generic so type inference will flow. Example: + * + * function product(a: number, b: number) { + * return a * b; + * } + * + * if (isFunction(fn)) { + * return fn(1, 2); + * } else { + * throw "Must provide the `product` function"; + * } + */ +export function isFunction(v: any): v is T { + return typeof v === 'function'; +} + +export function isCanActivate(guard: any): guard is CanActivate { + return guard && isFunction(guard.canActivate); +} + +export function isCanActivateChild(guard: any): guard is CanActivateChild { + return guard && isFunction(guard.canActivate); +} + +export function isCanDeactivate(guard: any): guard is CanDeactivate> { + return guard && isFunction>>(guard.canDeactivate); +}