When creating users with a name as a parameter from the client and assigning "created" on the server, I utilize Pick<User, "name">
. However, Typescript does not raise an error when a complete User
object is passed to that function:
interface User {
name: string;
created: Date;
}
const user: User = {
name: "Test User",
created: new Date()
};
const createUser = (user: Pick<User, "name">): User => ({
name: user.name,
created: new Date()
});
const newUser1 = createUser(user); // No error
const newUser2 = createUser({
name: "Another Test User",
created: new Date() // Error
});
Sandbox: https://codesandbox.io/s/unruffled-wave-tewh8
I find it surprising that the first case does not throw an error. Why is this so?