TypeScriptでの関数パラメーターの破棄

async function update({id, ...todoInfo }: ITodo) { // this line here
    const db = await makeDb()
    const foundTodo = await db.collection('todos').updateOne({ _id: transformId(id) }, { $set: { ...todoInfo } })
    return foundTodo.modifiedCount > 0 ? { _id: id, ...todoInfo } : null
  }

id属性は、req.paramsオブジェクトからのものを意味し、... todoInfoはreq.bodyオブジェクトからのものです。しかし、typescriptはエラーをスローしますプロパティIDはインターフェイスITodoに存在しません。どうすればこの問題を克服できますか?ITodoのインターフェースは次のようになります。

export interface ITodo {
  todo_name: string
  start_time: Date
  end_time: Date
  description: string
  priority: Priority
}

この方法を試しましたが、このようなオブジェクトのネストが発生しました。{todoInfo:{todo_name ...}}

async function update({id, ...todoInfo }: {id: string, todoInfo: ITodo}) {
    const db = await makeDb()
    const foundTodo = await db.collection('todos').updateOne({ _id: transformId(id) }, { $set: { ...todoInfo } })
    return foundTodo.modifiedCount > 0 ? { _id: id, ...todoInfo } : null
  }

id属性をインターフェースに追加したくないのは、それをどこでも使用するか、オプションにすることができるためです。これにより、id属性を未定義にできないため、他の関数呼び出しが台無しになります。どうもありがとうございました。

おすすめ

転載: blog.csdn.net/diaojw090/article/details/114011220