refactor(router): add type guards for router guards (#26521)

PR Close #26521
This commit is contained in:
Jason Aden 2018-10-03 15:32:52 -07:00 committed by Matias Niemelä
parent ad6771dcd4
commit 9f4299d47b
2 changed files with 43 additions and 3 deletions

View File

@ -374,6 +374,7 @@ export interface Resolve<T> {
*
* @publicApi
*/
export interface CanLoad {
canLoad(route: Route, segments: UrlSegment[]): Observable<boolean>|Promise<boolean>|boolean;
}
export interface CanLoad { canLoad: CanLoadFn; }
export type CanLoadFn = (route: Route, segments: UrlSegment[]) =>
Observable<boolean>| Promise<boolean>| boolean;

View File

@ -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<product>(fn)) {
* return fn(1, 2);
* } else {
* throw "Must provide the `product` function";
* }
*/
export function isFunction<T>(v: any): v is T {
return typeof v === 'function';
}
export function isCanActivate(guard: any): guard is CanActivate {
return guard && isFunction<CanActivate>(guard.canActivate);
}
export function isCanActivateChild(guard: any): guard is CanActivateChild {
return guard && isFunction<CanActivateChild>(guard.canActivate);
}
export function isCanDeactivate(guard: any): guard is CanDeactivate<Type<any>> {
return guard && isFunction<CanDeactivate<Type<any>>>(guard.canDeactivate);
}