Skip to content

False or misleading errors with comparing the same types #15475

Closed
@zdrummond

Description

@zdrummond

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 lets) 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

  1. Why is the errors about types, when both sides are strings?
  2. Why does "foo" === "foo" work fine, but not "boo" === "foo"?
  3. 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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions