我有枚举:

export enum 算法rithmEnum {
    SHA1,
    SHA256,
    SHA512
}

我将其用于一个类的属性算法:

export class Authenticator {
    Type: Type = Type.TOTP;
    Icon: string = '';
    Issuer: string = '';
    Username: string = '';
    Secret: string = '';
    Pin: null = null;
    算法rithm: 算法rithmEnum = 算法rithmEnum.SHA1;
    Digits: number = 6;
    Period: number = 30;
    Counter: number = 0;
    CopyCount: number = 0;
    Ranking: number = 0;

    constructor() {

    }
}

然后,在其中一个组件中,我try 将其与switch 一起使用:

exportUriList() {
    const authData = this.authDataService.authData;
    let uriList: string[] = [];

    authData.Authenticators.forEach(auth => {
      //otpauth://totp/IBM%20Security%20Verify:@USER_NAME@?secret=@SECRET_KEY@&issuer=IBM%20Security%20Verify&algorithm=@ALGORITHM@

      let issuerAndUsername = `${auth.Issuer}:${auth.Username}`;
      let secret = `?secret=${auth.Secret}`;
      let issuer = `&issuer=${auth.Issuer}`;
      let algorithm = '';

      if (auth.算法rithm) {

        switch (auth.算法rithm) {
          case 算法rithmEnum.SHA1:
            break;
          case 算法rithmEnum.SHA256:
            break;
          case 算法rithmEnum.SHA512:
            break;
        }

        algorithm = `&algorithm=${auth.算法rithm}`;
      }

      let uri = 'otpauth://totp/';

    });
}

Seems like should work, right? It doesn't: enter image description here Why I get this error? It looks like typescript missed one of the enums and there is no proper type?

Type '算法rithmEnum.SHA1' is not comparable to type '算法rithmEnum.SHA256 | 算法rithmEnum.SHA512'.

推荐答案

由于注释标记的条件,您的代码显式筛选出值sha1:

exportUriList() {
    const authData = this.authDataService.authData;
    let uriList: string[] = [];

    authData.Authenticators.forEach(auth => {
      //otpauth://totp/IBM%20Security%20Verify:@USER_NAME@?secret=@SECRET_KEY@&issuer=IBM%20Security%20Verify&algorithm=@ALGORITHM@

      let issuerAndUsername = `${auth.Issuer}:${auth.Username}`;
      let secret = `?secret=${auth.Secret}`;
      let issuer = `&issuer=${auth.Issuer}`;
      let algorithm = '';

      // THIS FILTERS OUT THE VALUE 0, WHICH IS SHA1
      if (auth.算法rithm) {

        switch (auth.算法rithm) {
          case 算法rithmEnum.SHA1:
            break;
          case 算法rithmEnum.SHA256:
            break;
          case 算法rithmEnum.SHA512:
            break;
        }

        algorithm = `&algorithm=${auth.算法rithm}`;
      }

      let uri = 'otpauth://totp/';

    });
}

之所以会出现这种情况,是因为枚举中的sha1获得了分配的数值0.在IF条件中,数值0(如空字符串)的计算结果为FALSE.因此,TypeScrip缩小了文字范围,将SHA1排除在外.

将IF条件更改为

if (typeof auth.算法rithm === "number") {

取而代之的是.

Typescript相关问答推荐

如何获取数组所有可能的索引作为数字联合类型?

typescribe不能使用值来索引对象类型,该对象类型满足具有该值的另一个类型'

使用React处理Websocket数据

如何判断输入是否是TypeScript中的品牌类型?

如何访问Content UI的DatePicker/中的sx props of year picker?'<>

使Typescribe强制所有对象具有相同的 struct ,从两个选项

更新为Angular 17后的可选链运算符&?&问题

Next.js错误:不变:页面未生成

如果字符串文字类型是泛型类型,则用反引号将该类型括起来会中断

如果数组使用Reaction-Hook-Form更改,则重新呈现表单

如何在Angular 服务中制作同步方法?

为什么上下文类型在`fn|curred(Fn)`的联合中不起作用?

TypeScrip-如何自动推断UNION变量的类型

如何在Reaction Native中关注屏幕时正确更新状态变量?

如何使对象同时具有隐式和显式类型

如何为对象数组中的每个条目推断不同的泛型

Typescript 和 Rust 中跨平台的位移数字不一致

React-Router V6 中的递归好友路由

如何为字符串模板文字创建类型保护

当受其他参数限制时,通用参数类型不会缩小