不允许使用类型始终为真或始终为假的表达式作为判断条件。
const index = 0;
export function head(items: readonly string[]): string {
// Necessary, since items.length might be 0
if (items.length) {
return items[index].toUpperCase();
} else {
return '';
}
}
export function foo(arg: string): void {
// Necessary, since foo might be ''.
if (arg) {
}
}
export function bar(arg?: string | null) {
// Necessary, since arg might be nullish
return arg?.length;
} const index = 0;
export function head(items: readonly string[]) {
// items can never be nullable, so this is unnecessary
if (items) {
return items[index].toUpperCase();
} else {
return '';
}
}
export function foo(arg: 'bar' | 'baz') {
// arg is never nullable or empty string, so this is unnecessary
if (arg) {
}
}
export function bar(arg: string) {
// arg can never be nullish, so ?. is unnecessary
return arg?.length;
}