【发布时间】:2018-05-01 01:50:45
【问题描述】:
我有这种类型:
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
}
【问题讨论】:
-
BunionOpts长什么样子? -
谢谢我刚刚添加了该信息
-
有
BunionLevel&BunionLevels吗? -
从您发布的代码看来,String 可能不是有效的
BunionLevel值;事实上,如果这真的是 exact 代码,它永远不会是。 -
除了“BunionLevels”中的拼写错误外,它们看起来不兼容 - 例如,您的字符串有
null和''的大小写。它还有toUpperCase,而BunionLevel没有一个大写字母。
标签: javascript node.js typescript typescript2.0