Maximus Maximus - 1 year ago 66
TypeScript Question

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

defines the following in the

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 Source

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

 export declare type Routes = Route[];


 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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download