在通用声明中破坏的用途是什么?

发布于 2025-01-24 18:14:35 字数 364 浏览 1 评论 0原文

破坏可能是错误的术语...

在我正在工作的代码库中找到了这一点。这令人困惑。

await apolloClient
    .query<{ Workorder: Workorder }>({
      query: WORKORDER_QUERY,
      variables: { uuid: id },
    })

我看不到{workorder:workorder}的点。这里发生了什么?该vs .query&lt; workorder&gt;的值是什么?

Workorder是一个大约30个属性的接口。

Destructuring might be the wrong term...

Found this in the codebase I'm working in. It's perplexing.

await apolloClient
    .query<{ Workorder: Workorder }>({
      query: WORKORDER_QUERY,
      variables: { uuid: id },
    })

I don't see the point of { Workorder: Workorder }. What is happening here? What is the value of that vs .query<Workorder>?

Workorder is an interface with about 30 properties.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雾里花 2025-01-31 18:14:35

这不是破坏性的。这只是意味着要传递的类型参数是具有工作表属性的对象,并且该属性的值是工作派。

例如,给定的:

type Workorder = { order: boolean };
declare function fn<T>(): T;

const result = fn<Workorder>();

结果是类型:而

{
  order: boolean
}

如果执行结果,则类型为类型,而

const result = fn<{ Workorder: Workorder }>();

结果现在是类型:

{
  Workorder: {
    order: boolean
  }
}

不是.query 做同样的事情,但这会让您了解区别是什么。执行&lt; {workorder:workorder}&gt;只需将工作端包装在对象中,然后将其作为类型参数传递。

It's not destructuring. It just means that the type argument being passed is an object with a Workorder property, and that the value for that property is a Workorder.

For example, given:

type Workorder = { order: boolean };
declare function fn<T>(): T;

const result = fn<Workorder>();

where the type parameter is the same type that's returned, the result is of type:

{
  order: boolean
}

Whereas if you do

const result = fn<{ Workorder: Workorder }>();

the result is now of the type:

{
  Workorder: {
    order: boolean
  }
}

Not that .query does the same thing, but that'll give you an idea of what the difference is. Doing <{ Workorder: Workorder }> just wraps the Workorder in an object and passes it as a type parameter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文