TypeScript "object" doesn't make sense
Briefly

TypeScript "object" doesn't make sense
"You'll often hear that "everything is an object" in JavaScript - but that's not quite true. Primitives like string, number, or boolean are not objects. The confusion likely comes from the fact that primitives behave like objects in many cases. This is due to JavaScript's auto-boxing mechanism: Primitives have no methods but still behave as if they do. When properties are accessed on primitives, JavaScript auto-boxes the value into a wrapper object and accesses the property on that object instead. - MDN"
"TypeScript separates these two concerns. "How": Structural Typing Example Consider this logger function: function logLength(value: { length: number }) { if(typeof value === 'string') { console.log('String length is:', value.length); return; } if (Array.isArray(value)) { console.log('Array length is:', value.length); return; } console.log('Length is:', value.length);} This function accepts any value that has a length: number property. logLength('Hello world!') // String length is: 12logLength(['Hello', 'world!']) // Array length is: 2"
Primitives in JavaScript (string, number, boolean) are not objects, but they often behave like objects because of auto-boxing. When a property is accessed on a primitive, JavaScript auto-boxes the primitive into a corresponding wrapper object and performs the property access on that object. Primitive values report different typeof results than their object counterparts and do not retain added properties. TypeScript separates a value's shape (how it can be used) from its type (what it is) and employs structural typing so functions accept any value matching the required shape, such as a length:number property.
Read at Medium
Unable to calculate read time
[
|
]