我在Logger
类中获得了以下静态方法。我完全不了解tsserver
错误。
const enum LogLevel {
ERROR = 0,
WARN = 1
}
export default LogLevel;
static defaultHandler(level: LogLevel, ...message: readonly string[]) {
if (!Logger.isLevelEnabled(level)) return;
let handler: Function;
switch (level) {
case LogLevel.ERROR:
handler = console.error;
break;
case LogLevel.WARN:
handler = console.warn;
break;
}
// [tsserver 2454] Variable 'handler' is used before being assigned
handler(...message);
}
由于LogLevel
是一个枚举,因此永远不会取消分配处理程序。如果我添加if
,tslint
对此表示抱怨。
// [tslint 1] expression is always true (strict-type-predicates)
if (handler !== undefined) handler(...message);
我甚至删除了no-let
tslint
规则,因为它出于某种原因在switch
案例中看不到分配,并且希望我使用const handler: Function;
。谁能解释这是怎么回事。
答案 0 :(得分:3)
您有
let handler: Function;
switch (level) {
case LogLevel.ERROR:
handler = console.error;
break;
case LogLevel.WARN:
handler = console.warn;
break;
}
您仅处理ERROR
和WARN
。选项更多:
ERROR = 0,
WARN = 1,
INFO = 2,
DEBUG = 3,
VERBOSE = 4
添加默认设置以适应剩余情况:
let handler: Function;
switch (level) {
case LogLevel.ERROR:
handler = console.error;
break;
case LogLevel.WARN:
handler = console.warn;
break;
default:
handler = console.log;
break;
}