'Difference between assigning a value versus assigning another variable
I have two objects p and q. Object q has one extra property 'b':
let p = {a:1}
let q = {a:1, b:2}
Now, if I assign q to p. No problem:
p = q // ok
However, the following has type error:
p = {a:1, b:2} // not ok
Error:
Type '{ a: number; b: number; }' is not assignable to type '{ a: number; }'.
Object literal may only specify known properties, and 'b' does not exist in type '{ a: number; }'.
(2322)
In both cases, I am reassigning an object with extra properties to p. My understanding of duck-typing is that assigning objects are allowed as long as there is no missing property. But then why does the compiler complain in the latter case?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
