Consider this TypeScript (2.7.2) interface:
export interface Sample {
text?: string;
isActive?: boolean;
}
The following code snippet will output 'false' as expected:
let sample: Sample = {text: ''};
if (sample.text && sample.text.length > 0) {
sample.isActive = true;
} else {
sample.isActive = false;
}
console.log(sample.isActive);
However, this piece of code prints an empty string without any value:
let sample: Sample = {text: ''};
sample.isActive = sample.text && sample.text.length > 0;
console.log(sample.isActive);
If you are puzzled by the difference in behavior between the two examples, you're not alone. The second example should theoretically work like the first one, but it doesn't. What could be the reason for this inconsistency?