[Typescript] Namespaces
作者:互联网
In general, it is not recommeded to use Namespace in Typescript, we should use ES6 module export import syntax.
But there are still some good usecases for Namespace, for example when you defining Actions in Ngrx or Ngxs. You can group action by namespace.
Considering two example:
Without namespace:
export class AddTodo { static readonly type = '[Todo] Add'; constructor(public payload: any) {} } export class EditTodo { static readonly type = '[Todo] Edit'; constructor(public payload: any) {} } export class FetchAllTodos { static readonly type = '[Todo] Fetch All'; } export class DeleteTodo { static readonly type = '[Todo] Delete'; constructor(public id: number) {} }
With namespace:
export namespace Todo { export class Add { static readonly type = '[Todo] Add'; constructor(public payload: any) {} } export class Edit { static readonly type = '[Todo] Edit'; constructor(public payload: any) {} } export class FetchAll { static readonly type = '[Todo] Fetch All'; } export class Delete { static readonly type = '[Todo] Delete'; constructor(public id: number) {} } }
标签:Typescript,type,readonly,export,Namespaces,static,Todo,class 来源: https://www.cnblogs.com/Answer1215/p/12202184.html