Several past discussions on SO have touched upon the concept that the inferred type from &&
is based on the last expression.
- TypeScript’s failure to detect union type with an && operator
- Uncovering the reason behind the && operator outputting the type of the second operand
However, TypeScript version 2.4.2 raises an error for the following code:
function isQuerySql(sql: string): boolean {
return sql && _.trimStart(sql).toLowerCase().startsWith('select');
}
Error TS2322: Type 'boolean | ""' cannot be assigned to type 'boolean'. Type '""' cannot be assigned to type 'boolean'.
The issue at hand is unclear. The expression
_.trimStart(sql).toLowerCase().startsWith('select')
should be inferred as boolean. So, where does ""
factor into this?