0
一些先前SO问题描述了从&&
推断出的类型是最后一个表达式的类型的类型。打字稿&&操作者不产生第二操作数
- TypeScript does not detect union type with an && operator
- Why does the && operator produce the type of the second operand
但打字稿2.4.2下面的代码错误报告:
function isQuerySql(sql: string): boolean {
return sql && _.trimStart(sql).toLowerCase().startsWith('select');
}
错误TS2322:类型“布尔| “”'不能分配给'boolean'类型。 类型“‘’”是不能分配给输入“布尔”。
我无法弄清楚什么是错的。 _.trimStart(sql).toLowerCase().startsWith('select')
被推断为布尔值。哪里""
来?