Maximus Maximus - 22 days ago 6
TypeScript Question

What's the advantage of having a custom type that is simply an array of a another custom type objects

Angular2
defines the following in the
@angular\router\src\config.d.ts
:

export declare type Routes = Route[];


So then it can be used like this:

import { Routes } from "@angular/router";

export const routes: Routes = [...]


I don't really understand why create this custom type if I can simply do like this:

import {Route} from "@angular/router";

export const routes: Route[] = [];


Which seems much more readable to me.

Answer

That's mostly a matter of preference, but there is an advantage that you can change

 export declare type Routes = Route[];

to

 export declare type Routes = Stack<Route>;
 export declare type Routes = Set<Route>;

without replacing Route[] everywhere throughout your application code but the whole application is still using a different type.

It might be advantageous to have more intention revealing names, but using made up type names for collections types can also make it harder to read your code because Routes doesn't reveal that its actually an array of routes.