字符串与字符串枚举不兼容

亚历山大·米尔斯

我有这种类型:

export type BunionLevel = 'foo' | 'bar' | 'baz';

然后我有这门课:

export class BunionLogger {

  level: BunionLevel;

  constructor(opts?: BunionOpts) {
    this.level = String((opts && (opts.level || opts.maxlevel) || maxLevel || '')).toUpperCase();
  }

}

我得到这个转译错误:

在此处输入图片说明

呃我该怎么办?我不知道如何继续。我可以做这个:

this.level = <BunionLevels> String((opts && (opts.level || opts.maxlevel) || maxLevel || '')).toUpperCase();

但演员似乎没有必要......?

根据要求,BunionOpts看起来像:

export interface BunionOpts {
  level?: BunionLevel
  maxlevel?: BunionLevel
  appName?: string
  name?: string
  fields?: object
}
提香·切尔尼科娃-德拉戈米尔

如果您使用该String函数,则 的结果String((opts && (opts.level || opts.maxlevel) || maxLevel || ''))string不是 的值BunionLevel此外,由于您提供了''作为默认值并且您使用toUpper的结果肯定不是BunionLevel.

如果您删除StringtoUpper提供有效的默认值,则一切正常:

this.level = (opts && (opts.level || opts.maxlevel)) || 'foo';

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章