In a new reactjs project I have this basic code:
type User = {
name: ?string,
age: ?string,
};
const getUserName = ({name,age,}: User): ?string => name;
giving me this error:
TypeError: Cannot destructure property 'name' of 'undefined' as it is undefined.
What does this mean?
Am I not defining both User and its name just above?! Why is it saying I'm not?
Intention is:
const someUser = { name: "John", age: "12" }
const userName = getUserName(someUser)
console.log(userName)// should output 'John'
getUserNamecurrently you are not passing any params()is the arguments for your arrow function. Here you're destructuring the arguments of an input object, which has the type ofUser.objectof the shape{name, age}and you are destructuring that object. You want to use this function asgetUserName({ name: "John", age: 12 })