我有这种类型:
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
.
如果您删除String
并toUpper
提供有效的默认值,则一切正常:
this.level = (opts && (opts.level || opts.maxlevel)) || 'foo';
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句