Repository navigation
False or misleading errors with comparing the same types #15475
Description
Activity
blakeembrey commented
on Apr 30, 2017 ContributorMore actions- The string is literal type
- Because the type of
"foo"can only equal"foo"and never"boo" - Casting to string treats it as a type of
stringinstead of a string literal
You can enable
noUnusedLocalsto be warned when something is unused by the compiler. As for the_.getissue, check the signature of the method in the declaration file - it's possible it's using a generic and inferring"foo"but if that's wrong, you can override it to the type you want (something like_.get<string>(...)).- addedWorking as IntendedThe behavior described is the intended behavior; this is not a bugThe behavior described is the intended behavior; this is not a bug
on Apr 30, 2017 DanielRosenwasser commented
on Apr 30, 2017 MemberMore actionsThanks for answering Blake Embrey (@blakeembrey)!
Reacted by Blake EmbreyThanks 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
<string>togetit works 👍However, I don't understand is I am comparing to primitives, as I am in
let a = "boo" === "foo"
there should be no boxing, right? So, its not like the left is primative, and the right is an object, right? So it just return false and be done.Also, with answer # 2, why does it matter if it's going to be always true, or always false? Why is true treated special?
Zachary Drummond (@zdrummond) TypeScript has "literal types" for string, number and boolean, so that the type of a string literal
"foo"is not actuallystringbut literally"foo". In other words, this is a type error:let a: "foo" = "foo"; a = "bar"; // type error
That's the cause of the errors you're seeing.
falsandtru (@falsandtru) Mind blown! It all clicks into place. Thanks for helping me understand.
Daniel Rosenwasser (@DanielRosenwasser) Blake Embrey (@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!
- locked and limited conversation to collaborators
on Jun 14, 2018
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=trueNote: 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 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"?stringfix it?and a bonus, why is
lodashgetting sucked into this. (note, the lodash is my actual production issue, the rest was just trying to explore the problem)