Can someone please explain the difference between Boolean and boolean in Typescript?
Uppercase Boolean is an object type. Lowercase boolean is a primitive type. You should always use boolean (the primitive type in your programs). This is because, the Typescript type system does not force an object to its primitive type, while JavaScript does.
boolean is a primitive and Boolean is as object wrapper. So boolean, is the type whose values are either true or false while the other is an object. If you wanted to convert a string to a boolean you could try Boolean.
It is recommended that you use the Boolean() function to convert a value of a different type to a Boolean type, but you should never use the Boolean as a wrapper object of a primitive boolean value.
The boolean is a primitive type in Typescript. It represents a simple true/false value. They are implemented as numerical values with a single binary digit (i.e., 0 & 1). The Boolean is an object wrapper for the primitive boolean value.
Uppercase Boolean
is an object type.
Lowercase boolean
is a primitive type.
You should always use boolean
(the primitive type in your programs).
This is because, the Typescript type system does not force an object to its primitive type, while JavaScript does.
You shouldn't write:
function yourFunction(foo: Boolean)
But instead always write:
function yourFunction(foo: boolean)
For more info, see TypeScript Lang - Everyday Types
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With