'Why when i have the object in other variable - it's type is not recognized?
I have this function where i am making fullName of some object
interface Person {
firstName: string;
lastName: string;
}
makeFullName(obj: Person) {
return {
...obj,
fullName: `${obj.firstName} ${obj.lastName}`
}
}
so when i try to send object that is diffrent than the type
let obj = this.makeFullName({ firstName: 'Williams', lastName: 'John', age: 23 });
i get expected error
Argument of type '{ firstName: string; lastName: string; age: number; }' is not assignable to parameter of type 'Person'.
because i don't have age as as type in Person.
but when i do the same in other variable
let user = { firstName: 'Williams', lastName: 'John', age: 23 }
let obj = this.makeFullName(user);
i don't get that error. Why is that ? When i hover on user variable i still get the inferred type
firstName: string;
lastName: string;
age: number;
why it is working on this way ?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
