In my coding project, there is a function called sayMyName that takes an argument of type User
When I pass a user as reference, everything works perfectly:
interface User{
firstName:string;
lastName:string;
}
function sayMyName(user:User){
console.log(user.firstName + " " + user.lastName );
}
const user = {firstName: "john", lastName: "snow", age: 22}
sayMyName(user);
However, if I try to pass an object directly to sayMyName like this:
sayMyName({firstName: "john", lastName:"snow", age:22});
I encounter an error message:
Argument of type '{ firstName: string; lastName: string; age: number; }' is not assignable to parameter of type 'User'. Object literal may only specify known properties, and 'age' does not exist in type 'User'.
This issue has me puzzled - what exactly is causing the problem in my code?