简体   繁体   English

类型“WriteableDraft”上不存在属性“id”<note> | 可写草稿<{}></note>

[英]Property 'id' does not exist on type 'WriteableDraft<Note> | WritableDraft<{}>

I am converting my ReactJS / Redux Toolkit app to Typescript. One issue that I am facing is I am not able to get the properties of the state object:我正在将我的 ReactJS / Redux Toolkit 应用程序转换为 Typescript。我面临的一个问题是我无法获取 state object 的属性:

const useUpdatedData = (
  setNoteDescription: React.Dispatch<React.SetStateAction<string>>,
  setNoteData: React.Dispatch<
    React.SetStateAction<
      {
        fileName: string
        content: string
      }[]
    >
  >
) => {
  const { note } = useAppSelector((state) => state.notes)
  const { noteId } = useParams()

  useEffect(() => {
    if (noteId && noteId === **note.id**) {
      const filesData = filesObjectToArray(**note?.files**)
      const filesArray = filesData?.map((file) => {
        return {
          fileName: **file.filename**,
          content: **file.content**,
        }
      })

      setNoteDescription(note?.description)

      setNoteData(filesArray)
    }
  }, [noteId])
}

I have highlighted them with **.我用 ** 突出显示了它们。

Here's what the Interface looks like:这是界面的样子:

export interface Note {
  url: string
  id: string
  public: boolean
  created_at: string
  updated_at: string
  description: string
  files: {
    filename: {
      filename: string
      content: string
    }
  }
  owner: {
    login: string
    id: string
    avatar_url: string
  }
}

And here's the initial state:这是最初的 state:

interface NoteState {
  notes: Note[] | []
  userNotes: Note[] | []
  note: Note | {}
  searchedNote: Note | {}
  snackbar: {
    message: string
    type: string
    isOpen: boolean
  }
  forks: number
  deletedNote: string | null
  isSuccess: boolean
  isLoading: boolean
  isError: boolean
  isSearchError: boolean
  isStarred: boolean | undefined
  isForked: boolean | undefined
  isCreated: boolean
  isUpdated: boolean
  message: string | undefined
}

const initialState: NoteState = {
  notes: [],
  userNotes: [],
  note: {},
  searchedNote: {},
  snackbar: { message: "", type: "success", isOpen: false },
  forks: 0,
  deletedNote: null,
  isSuccess: false,
  isLoading: false,
  isError: false,
  isSearchError: false,
  isStarred: false,
  isForked: false,
  isCreated: false,
  isUpdated: false,
  message: "",
}

Clearly, the id property exists in the interface then why does it say it doesnt?明明接口里有id属性,为什么说不存在呢?

note can be Note | {} note可以Note | {} Note | {} , that is a Note or an empty object. TypeScript is not letting you access the id property since it may not exist. Note | {} ,即Note或空的 object。TypeScript 不允许您访问id属性,因为它可能不存在。

You can make this work by adding another check 'id' in note , which will narrow down the type to Note and then you can access .id :您可以通过'id' in note完成这项工作,这会将类型缩小为Note然后您可以访问.id

if (noteId && 'id' in note && noteId === note.id) {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM