Closed
Description
TypeScript Version: 2.3.1
Code
import * as _ from 'lodash'
var testObject = { 'a': [{ 'b': { 'c': 'boo' } }] };
if( _.get(testObject, 'a[0].b.c', 'foo') === 'boo') { //Error: TS2365
let a = "boo" === "foo" //Error: TS2365
let b = ("boo" as string) === "foo"
let c = "foo" === "foo"
console.log(`My results are a=${a}, b=${b} and c=${c}`)
}
Expected behavior:
No type related errors, and the resulting code outputs
> My values are a=false, b=false and c=true
Note: I would be fine if tsc produced errors (or better yet warned) about the no-op lines (all the let
s) and let me know I was doing work for no reason. It does not do this
Actual behavior:
I get two errors both on the line listed above with the comment //Error: TS2365
. The errors are
test.ts(4,5): error TS2365: Operator '===' cannot be applied to types '"foo"' and '"boo"'.
test.ts(5,11): error TS2365: Operator '===' cannot be applied to types '"boo"' and '"foo"'.
I have three questions
- Why is the errors about types, when both sides are strings?
- Why does
"foo" === "foo"
work fine, but not"boo" === "foo"
? - Why does casting to
string
fix it?
and a bonus, why is lodash
getting sucked into this. (note, the lodash is my actual production issue, the rest was just trying to explore the problem)