【发布时间】:2018-01-06 19:53:43
【问题描述】:
之前的一些 SO 问题描述了从 && 推断的类型是最后一个表达式的类型。
- TypeScript does not detect union type with an && operator
- Why does the && operator produce the type of the second operand
但 TypeScript 2.4.2 报告以下代码错误:
function isQuerySql(sql: string): boolean {
return sql && _.trimStart(sql).toLowerCase().startsWith('select');
}
错误 TS2322: 类型 'boolean | ""' 不能分配给类型 'boolean'。 类型 '""' 不能分配给类型 'boolean'。
我不知道出了什么问题。 _.trimStart(sql).toLowerCase().startsWith('select') 被推断为布尔值。 ""哪里来的?
【问题讨论】:
标签: typescript