![](/img/trans.png)
[英]TypeError: Cannot destructure property 'name' of 'item' as it is undefined
[英]TypeError: Cannot destructure property 'name' of 'undefined' as it is undefined
type User = {
name: ?string,
age: ?string,
};
const getUserName = ({name,age,}: User): ?string => name;
給我這個錯誤:
TypeError: Cannot destructure property 'name' of 'undefined' as it is undefined.
這是什么意思?
我不是在上面定義了 User 和它的名字嗎? 為什么說我不是?
意圖是:
const someUser = { name: "John", age: "12" }
const userName = getUserName(someUser)
console.log(userName)// should output 'John'
添加流程:
npm install --save flow-bin
要么
yarn add flow-bin
。
謝謝大家的好幫手! 事實證明,一旦添加到這里就缺少流程,一切正常
工作代碼的最終版本是:
type User = {
name: ?string,
age: ?string,
};
const getUserName = ({name,age,}: User): ?string => name;
const someUser = { name: "John", age: "12" }
const userName = getUserName(someUser)
console.log(userName)// should output 'John'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.