什么具有是一个简单的另一自定义类型的阵列的自定义类型的优点对象
问题描述:
Angular2
定义了@angular\router\src\config.d.ts
以下:什么具有是一个简单的另一自定义类型的阵列的自定义类型的优点对象
export declare type Routes = Route[];
所以那么就可以像这样使用:
import { Routes } from "@angular/router";
export const routes: Routes = [...]
我真的不明白为什么创建这个定制的类型,如果我可以简单地这样做:
import {Route} from "@angular/router";
export const routes: Route[] = [];
对我而言,这看起来更可读。
答
这主要是偏好的问题,但有一个好处,你可以改变
export declare type Routes = Route[];
到
export declare type Routes = Stack<Route>;
export declare type Routes = Set<Route>;
无需更换Route[]
无处不在整个应用程序的代码,但整个应用程序仍在使用一个不同的类型。
有更多的意图显示名称可能是有利的,但使用组合类型名称的集合类型也可以使其更难以阅读您的代码,因为Routes
没有显示它实际上是一个路由数组。
没错,谢谢,阻止我使用'Routes'而不是'Route []'的方法之一就是没有创建这种类型的方法,并且这个'routes:Routes = [...]'仍然显示这是一个数组。如果我将原始代码更改为'Routes = Stack;',那么会发生什么? –