访问数组属性的编译错误

桑德罗·金

我已经声明了以下类型:

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属性,而不是实际的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])

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章