[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) {}
  }
}

 

上一篇:Dockerfile-Namespace


下一篇:带有命名空间的XML数据绑定