-
Notifications
You must be signed in to change notification settings - Fork 12.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
False or misleading errors with comparing the same types #15475
Comments
You can enable |
Thanks for answering @blakeembrey! |
Thanks for the quick response! So if I understand correctly, this is about boxing. As far as I could find, a "string literal" is a primitive and String is an object. And JS will auto-box the primitive when it needs to (i..e when it needs to call a function on a string for example). So if we take the lodash get function with a default, I can imagine somewhere in there it might box the string, and in fact if I do as you suggest and add the However, I don't understand is I am comparing to primitives, as I am in Also, with answer # 2, why does it matter if it's going to be always true, or always false? Why is true treated special? |
@zdrummond TypeScript has "literal types" for string, number and boolean, so that the type of a string literal let a: "foo" = "foo";
a = "bar"; // type error That's the cause of the errors you're seeing. |
@falsandtru Mind blown! It all clicks into place. Thanks for helping me understand. @DanielRosenwasser @blakeembrey Thanks for your help as well! I really appreciate the core team helping with what really ends up being support. I wonder if there is a way to make that more clear to newcomers. For example, Ruby linter has a nice description for each error, so you can understand the rational (random example https://github.com/bbatsov/ruby-style-guide#concat-strings). I know a linter is different then a compiler, but it feels like if there was a link-out to common issues it would be really nice. To that point, y'all put an error number (in this case TS2365) on it, but Google only finds Github Issues and Stack Overflow response; if there are docs or more details they are hard to find. Thanks again! |
TypeScript Version: 2.3.1
Code
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 thisActual behavior:
I get two errors both on the line listed above with the comment
//Error: TS2365
. The errors areI have three questions
"foo" === "foo"
work fine, but not"boo" === "foo"
?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)The text was updated successfully, but these errors were encountered: