简体   繁体   English

泛型声明中解构的用途是什么?

[英]What is the use of destructuring in a generic declaration?

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 } .我不明白{ Workorder: Workorder }的意义。 What is happening here?这里发生了什么? What is the value of that vs .query<Workorder> ?那与.query<Workorder>的价值是多少?

Workorder is an interface with about 30 properties. Workorder是一个具有大约 30 个属性的接口。

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.它只是意味着传递的类型参数是一个带有 Workorder 属性的 object,并且该属性的值是一个 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:其中类型参数与返回的类型相同, result的类型为:

{
  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.并不是说.query做同样的事情,但这会让您了解不同之处。 Doing <{ Workorder: Workorder }> just wraps the Workorder in an object and passes it as a type parameter.执行<{ Workorder: Workorder }>只是将 Workorder 包装在 object 中并将其作为类型参数传递。

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

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