简体   繁体   English

如何导出多个类型别名?

[英]How to export multiple Type Aliases?

For better capsulation I want to create a new type that includes multiple other types. 为了更好地封装,我想创建一个包含多个其他类型的新类型。 entityService should be typed as OakService or MapleService which should be included inside TreeService . entityService类型应为OakServiceMapleService ,它们应包含在TreeService

One possible solition you can see inside the comments. 您可以在评论中看到一种可能的隔离。 If I uncomment the imports inside garden.component.ts and replace TreeService with OakService | MapleService 如果我去掉里面garden.component.ts进口和替换TreeServiceOakService | MapleService OakService | MapleService , it will work, but I want some kind of parent type, that includes them all. OakService | MapleService ,它将工作,但是我想要某种父类型,包括它们全部。

tree.service.ts tree.service.ts

import { OakService } from './oak.service';
import { MapleService } from './maple.service';

export type TreeService = typeof OakService | MapleService;

oak.service.ts Oak.service.ts

import { BaseService } from './base.service';

@Injectable()
export class OakService extends BaseService {
  constructor() {
    super();
  }
}

maple.service.ts maple.service.ts

import { BaseService } from './base.service';

@Injectable()
export class MapleService extends BaseService {
  constructor() {
    super();
  }
}

garden.component.ts garden.component.ts

// import { OakService } from './oak.service';
// import { MapleService } from './maple.service';
import { TreeService } from './parent.service';

@Component()
export class GardenComponent {
  constructor(
    protected entityService: TreeService // OakService | MapleService
  ) {}
}

balcony.component.ts balcony.component.ts

import { MapleService } from './maple.service';

export class BalconyComponent extends GardenComponent {   
  constructor(
    protected mapleService: MapleService
  ) {
    super(mapleService);   
  }
}

Info: The code above does not work. 信息:上面的代码不起作用。 Your have to use the code inside the comments. 您必须使用注释中的代码。 The error I've got is not inside balcony.component.ts : ERROR in : Can't resolve all parameters for BaseListComponent in .../garden.component.ts: (?). 我有错误是不是里面balcony.component.ts:错误:无法解析的所有参数BaseListComponent在... / garden.component.ts:(?)。

Parameter types in injectable class allow to skip Angular @Inject decorator in TypeScript and annotate a constructor for dependency injection. 可注入类中的参数类型允许跳过TypeScript中的Angular @Inject装饰器,并为依赖项注入注释构造函数。 Types don't exist at runtime and cannot be injected. 类型在运行时不存在,无法注入。

GardenComponent cannot be workable component because of that. GardenComponent无法成为可行的组件。 Since is used as abstract class, it shouldn't have @Component decorator: 由于用作抽象类,因此不应具有@Component装饰器:

export abstract class GardenComponent {
  constructor(
    protected entityService: TreeService // OakService | MapleService
  ) {}
}

While child classes should have @Component : 虽然子类应该具有@Component

@Component(...)
export class BalconyComponent extends GardenComponent {   
  constructor(mapleService: MapleService) {
    super(mapleService);
  }
}

Constructors are mandatory in child classes because parent class doesn't contain proper annotation, and their parameters shouldn't have visibility modifiers because this.entityService is already assigned by parent constructor. 构造函数在子类中是强制性的,因为父类不包含适当的注释,并且其参数不应具有可见性修饰符,因为this.entityService已由父构造函数分配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM