【草稿】在 Typescript 中从对象中动态解构接口类型

问题

interface A {
  title: string,
  description: string,
}

let a = {
  title: "title a",
  description: "description a",
  url: "http://example.com/a"
}

是否可以直接将a转换为接口A的类型

\\ 伪代码
let b = {...a as A};
\\ 使得
b = {
  title: "title a",
  description: "description a"
}

参考链接

  1. https://stackoverflow.com/questions/51155239/remove-properties-from-an-object-that-are-not-declared-in-a-type-interface (redict to link 2)
  2. https://stackoverflow.com/questions/50839597/typescript-extract-interface-members-only-possible
posted @ 2022-11-18 15:36  reeink  阅读(52)  评论(0)    收藏  举报