refactor(router): add type definitions for guard functions (#26521)

PR Close #26521
This commit is contained in:
Jason Aden 2018-10-03 15:35:13 -07:00 committed by Matias Niemelä
parent 9f4299d47b
commit 6712492dc2
3 changed files with 29 additions and 12 deletions

View File

@ -10,7 +10,7 @@ import {Observable} from 'rxjs';
import {Route} from './config';
import {ActivatedRouteSnapshot, RouterStateSnapshot} from './router_state';
import {UrlSegment} from './url_tree';
import {UrlSegment, UrlTree} from './url_tree';
/**
@ -33,7 +33,7 @@ import {UrlSegment} from './url_tree';
* canActivate(
* route: ActivatedRouteSnapshot,
* state: RouterStateSnapshot
* ): Observable<boolean>|Promise<boolean>|boolean {
* ): Observable<boolean>|Promise<boolean>|boolean|UrlTree {
* return this.permissions.canActivate(this.currentUser, route.params.id);
* }
* }
@ -80,9 +80,12 @@ import {UrlSegment} from './url_tree';
*/
export interface CanActivate {
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot):
Observable<boolean>|Promise<boolean>|boolean;
Observable<boolean|UrlTree>|Promise<boolean|UrlTree>|boolean|UrlTree;
}
export type CanActivateFn = (route: ActivatedRouteSnapshot, state: RouterStateSnapshot) =>
Observable<boolean>| Promise<boolean>| boolean | UrlTree;
/**
* @description
*
@ -103,7 +106,7 @@ export interface CanActivate {
* canActivateChild(
* route: ActivatedRouteSnapshot,
* state: RouterStateSnapshot
* ): Observable<boolean>|Promise<boolean>|boolean {
* ): Observable<boolean>|Promise<boolean>|boolean|UrlTree {
* return this.permissions.canActivate(this.currentUser, route.params.id);
* }
* }
@ -160,9 +163,12 @@ export interface CanActivate {
*/
export interface CanActivateChild {
canActivateChild(childRoute: ActivatedRouteSnapshot, state: RouterStateSnapshot):
Observable<boolean>|Promise<boolean>|boolean;
Observable<boolean|UrlTree>|Promise<boolean|UrlTree>|boolean|UrlTree;
}
export type CanActivateChildFn = (childRoute: ActivatedRouteSnapshot, state: RouterStateSnapshot) =>
Observable<boolean|UrlTree>| Promise<boolean|UrlTree>| boolean | UrlTree;
/**
* @description
*
@ -185,7 +191,7 @@ export interface CanActivateChild {
* currentRoute: ActivatedRouteSnapshot,
* currentState: RouterStateSnapshot,
* nextState: RouterStateSnapshot
* ): Observable<boolean>|Promise<boolean>|boolean {
* ): Observable<boolean>|Promise<boolean>|boolean|UrlTree {
* return this.permissions.canDeactivate(this.currentUser, route.params.id);
* }
* }
@ -234,9 +240,14 @@ export interface CanActivateChild {
export interface CanDeactivate<T> {
canDeactivate(
component: T, currentRoute: ActivatedRouteSnapshot, currentState: RouterStateSnapshot,
nextState?: RouterStateSnapshot): Observable<boolean>|Promise<boolean>|boolean;
nextState?: RouterStateSnapshot): Observable<boolean|UrlTree>|Promise<boolean|UrlTree>|boolean
|UrlTree;
}
export type CanDeactivateFn<T> =
(component: T, currentRoute: ActivatedRouteSnapshot, currentState: RouterStateSnapshot,
nextState?: RouterStateSnapshot) => Observable<boolean>| Promise<boolean>| boolean | UrlTree;
/**
* @description
*
@ -374,7 +385,9 @@ export interface Resolve<T> {
*
* @publicApi
*/
export interface CanLoad { canLoad: CanLoadFn; }
export interface CanLoad {
canLoad(route: Route, segments: UrlSegment[]): Observable<boolean>|Promise<boolean>|boolean;
}
export type CanLoadFn = (route: Route, segments: UrlSegment[]) =>
Observable<boolean>| Promise<boolean>| boolean;

View File

@ -7,7 +7,7 @@
*/
import {Type} from '@angular/core';
import {CanActivate, CanActivateChild, CanDeactivate} from '../interfaces';
import {CanActivate, CanActivateChild, CanDeactivate, CanLoad} from '../interfaces';
/**
* Simple function check, but generic so type inference will flow. Example:
@ -26,6 +26,10 @@ export function isFunction<T>(v: any): v is T {
return typeof v === 'function';
}
export function isCanLoad(guard: any): guard is CanLoad {
return guard && isFunction<CanLoad>(guard.canLoad);
}
export function isCanActivate(guard: any): guard is CanActivate {
return guard && isFunction<CanActivate>(guard.canActivate);
}

View File

@ -52,15 +52,15 @@ export declare class ActivationStart {
}
export interface CanActivate {
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean;
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree;
}
export interface CanActivateChild {
canActivateChild(childRoute: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean;
canActivateChild(childRoute: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree;
}
export interface CanDeactivate<T> {
canDeactivate(component: T, currentRoute: ActivatedRouteSnapshot, currentState: RouterStateSnapshot, nextState?: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean;
canDeactivate(component: T, currentRoute: ActivatedRouteSnapshot, currentState: RouterStateSnapshot, nextState?: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree;
}
export interface CanLoad {