gpt4 book ai didi

typescript - @graphql-codegen 创建的 `Exact` 类型的用途是什么?

转载 作者:行者123 更新时间:2023-12-05 01:57:29 25 4
gpt4 key购买 nike

GraphQL Code Generator在创建的 TypeScript 文件的顶部创建此类型:

export type Exact<T extends { [key: string]: unknown }> = { [K in keyof T]: T[K] };

并将其用于所有客户端创建的查询变量:

src/foo.graphql :

query Foo($id: ID!) {
foo(id: $id) {
bar
}
}

generated/foo.ts :

...

export type Exact<T extends { [key: string]: unknown }> = { [K in keyof T]: T[K] };

...

export type FooQueryVariables = Exact<{
id: Scalars['ID'];
}>;

...

这个Exact<T>的目的是什么?类型及其影响 FooQueryVariables (相对于如果它不存在)?


https://www.graphql-code-generator.com/#live-demo 的完整演示:

schema.graphql :

schema {
query: Query
}

type Query {
foo(id: ID!): Foo
}

type Foo {
bar: String!
}

operation.graphql :

query Foo($id: ID!) {
foo(id: $id) {
bar
}
}

codegen.yml :

generates:
operations-types.ts:
plugins:
- typescript
- typescript-operations

已生成 operations-types.ts :

export type Maybe<T> = T | null;
export type Exact<T extends { [key: string]: unknown }> = { [K in keyof T]: T[K] };
export type MakeOptional<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]?: Maybe<T[SubKey]> };
export type MakeMaybe<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]: Maybe<T[SubKey]> };
/** All built-in and custom scalars, mapped to their actual values */
export type Scalars = {
ID: string;
String: string;
Boolean: boolean;
Int: number;
Float: number;
};

export type Query = {
__typename?: 'Query';
foo?: Maybe<Foo>;
};


export type QueryFooArgs = {
id: Scalars['ID'];
};

export type Foo = {
__typename?: 'Foo';
bar: Scalars['String'];
};

export type FooQueryVariables = Exact<{
id: Scalars['ID'];
}>;


export type FooQuery = { __typename?: 'Query', foo?: Maybe<{ __typename?: 'Foo', bar: string }> };

最佳答案

它旨在使人们无法将具有任何附加属性(除了 id)的对象作为 FooQueryVariables 传递。但它没有这样做:https://github.com/dotansimha/graphql-code-generator/issues/4577

关于typescript - @graphql-codegen 创建的 `Exact<T>` 类型的用途是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69268390/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com