我已经声明了这种类型:
export type Maybe<T> = T | null;
export type HostelId = {
id: Scalars['String']
}
我在此函数中使用
book (hostel: Array<Maybe<HostelId>>) : boolean {
console.log (hostel.id[])
}
但是我收到这个编译错误
Property 'id' does not exist on type 'Maybe<HostelId>[]'.
最佳答案
您的代码遇到的唯一问题是您正在访问 id 属性 在 Array 对象上,而不是实际的 HostelId 对象上。
export type Maybe<T> = T | null;
export type HostelId = {
id: String
}
function book (hostel: Array<Maybe<HostelId>>) : boolean {
hostel.map((h) => {
// I wrote this if statement in order to suppress the
// "Object is possibly 'null'." error
if (h != null && h.id != null){
console.log(h.id)
}
})
}
let x : HostelId = {id: 'asdf'}
book([x])
关于javascript - 访问数组属性时出现编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59561204/