-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Unsoundness in coercion from object to boolean #31464
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
Comments
Isn't the unsoundness the very first line
Coercing a primitive (number) to an object? |
At a single glance it seems TypeScript should distinguish E.g. when we annotate a certain variable as Number specifically, TS should regard it as an object with some props & methods it should have but otherwise as if it'd be a real primitive value same as other languages. |
It is not coercing a primitive to an object. The type If you want to enforce having an object, then you can make use of the
|
Is this bad a joke?
Which means it is an object. Primitives don't have properties. Your comment is factually sooo wrong... do we really need to discuss the very basics of JS? (Rhetorical question)
@uhyo Why do you upvote factually obviously wrong stufff? Just curious. Please learn the very basics of the language, it seems you have serious gaps if you think primitives in JS have properties. https://javascriptweblog.wordpress.com/2010/09/27/the-secret-life-of-javascript-primitives/ |
I was not precise enough. It doesn't has to be a property, but anything with the name 'toFixed', and numbers have a function named like that.
|
OPs example has a primitive number!! Not an object! Primitive number 0 is not an object without coercion, in this language! I really don't see the point of discussing language basics. You can learn all about primitives and objects in JS in free introductory Javascript courses. |
My example has a primitive number as well. I just omitted the type information. My example does the same as his: store 0 in a variable. |
Which is coerced to an object. You do understand JS automatically does type coercion?
A primitive number is not an object. Yes, you can coerce it to be one, news at 11. I thought you use static typing to not use such language features. https://javascriptweblog.wordpress.com/2010/09/27/the-secret-life-of-javascript-primitives/ See under
Your example explained right there, only using a string primitive instead of a number primitive. Enough already, I'm out. |
@lll000111 take it down a notch; we don't need to be condescending to have a discussion. Anyway, as to the OP: Types written as |
Uh oh!
There was an error while loading. Please reload this page.
TypeScript Version: 3.4.3, [email protected]
Search Terms: unsound coercion object boolean
Code
Expected behavior:
Compile error.
Actual behavior:
This code compiles and causes a runtime error when run.
TypeScript assumes that any value with object type (except
{}
) is truthy, but it's not true as illustrated above. If one of0
,""
,false
or0n
is assignable to an object type, it should not be considered truthy.Playground Link: https://www.typescriptlang.org/play/#src=const%20obj%3A%20%7B%20toFixed%3A%20unknown%20%7D%20%3D%200%3B%0A%2F%2F%20val%20has%20type%20%22123%22%0Aconst%20val%20%3D%20obj%20%26%26%20%22123%22%3B%0Aconsole.log(val.slice())%3B%0A%0A%0A
Related Issues: #18196 (fixed by #27157)
The text was updated successfully, but these errors were encountered: