简体   繁体   English

Typescript/Angular:更新扩展类/组件中的 Getter/Setter 类型转换

[英]Typescript/Angular: Updating a Getter/Setter type casting in an extended class/component

I've attempted to dumb this down, so the use case for wanting this is less clear but the problem remains.我试图把它简化,所以想要这个的用例不太清楚,但问题仍然存在。

Problem: Being unable to expand/update/declare enums or types within a class问题:无法在 class 中扩展/更新/声明枚举或类型

Goal: Rather than rewriting the getter/setter in extended class's with the new type whenever 'ValidState' deviates from the default in the base class, is there a way to do this so the getter/setters would be aware of the new type?目标:每当“ValidState”偏离基础 class 中的默认值时,与其用新类型重写扩展类中的 getter/setter,有没有办法做到这一点,以便 getter/setter 知道新类型?

Example Base Class示例基础 Class

import { Component, OnInit } from '@angular/core';

export enum DefaultValidStates {
  LOADING = 'LOADING',
  READY = 'READY',
}

type ValidState = DefaultValidStates;
// Is there a way for this to be within a class so it can be updated in extended classes and still trigger type checks

@Component({
  selector: 'app-base',
  template: '<p>BaseComponent status = {{status}}</p>',
})
export class BaseComponent implements OnInit{
  private _status: ValidState | undefined;

  constructor(
  ) {
  }

  ngOnInit() {
    this.status = DefaultValidStates.LOADING;
    window.setTimeout(() => {
      this.status = DefaultValidStates.READY;
    }, 3000)
  }

  get status(): ValidState | undefined {
    return this._status;
  }

  set status(status: ValidState | undefined) {
    this._status = status;
  }
}

Example Extended Class示例扩展 Class

import { Component} from '@angular/core';
import {BaseComponent, DefaultValidStates} from "../base/base.component";

enum OtherValidStates {
  FATAL_ERROR = 'FATAL_ERROR',
}

type ValidState = DefaultValidStates | OtherValidStates;
// Is there someway to pass/use this new type so the base class knows of it

@Component({
  selector: 'app-other',
  template: '<p>OtherComponent status = {{status}}</p>',
})
export class OtherComponent extends BaseComponent{
  
  constructor() {
    super();
  }

  ngOnInit() {
    this.status = DefaultValidStates.LOADING;
    window.setTimeout(() => {
      this.status = OtherValidStates.FATAL_ERROR;
      // Because ValidState is what is set in BaseComponent this won't compile
      // Type 'OtherValidStates' is not assignable to type 'DefaultValidStates | undefined'.
    }, 3000)
  }
}

Cheers,干杯,

Edits: Attempting to clarify issue.编辑:试图澄清问题。

Possible Solution可能的解决方案

Base Class底座 Class

import { Component, OnInit } from '@angular/core';

export enum DefaultValidStates {
  LOADING = 'LOADING',
  READY = 'READY',
}

@Component({
  selector: 'app-base',
  template: '<p>BaseComponent status = {{status}}</p>',
})
export class BaseComponent<States = undefined, ValidStates = States> implements OnInit{

  constructor() {}

  ngOnInit() {
    this.status = DefaultValidStates.LOADING;
    window.setTimeout(() => {
      this.status = DefaultValidStates.READY;
    }, 3000)
  }

  private _status: DefaultValidStates | ValidStates | undefined;

  get status(): DefaultValidStates | ValidStates | undefined {
    return this._status;
  }

  set status(status: DefaultValidStates | ValidStates | undefined) {
    this._status = status;
  }
}

Extended扩展

import {Component, OnInit} from '@angular/core';
import {BaseComponent, DefaultValidStates} from "../base/base.component";

enum OtherValidStates {
  FATAL_ERROR = 'FATAL_ERROR',
}

@Component({
  selector: 'app-other',
  template: '<p>OtherComponent status = {{status}}</p>',
})
export class OtherComponent extends BaseComponent<OtherValidStates> implements OnInit {
  constructor() {
    super();
  }
  ngOnInit() {
    this.status = DefaultValidStates.LOADING;
    window.setTimeout(() => {
      this.status = OtherValidStates.FATAL_ERROR;
    }, 3000)
  }
}

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

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