Ste*_*ano 6 typescript graphql aws-appsync aws-amplify
我得到了一个从 AWS-Amplify GraphQL(我相信它使用 apollo-codegen)自动生成的 TypeScript 类型,如下所示:
export type GetNoteQuery = {
getNote: {
__typename: "Note",
id: string,
createdAt: string | null,
updatedAt: string | null,
title: boolean | null,
content: string | null,
} | null,
Run Code Online (Sandbox Code Playgroud)
我想生成一个“Note”的基本类型,用作“基本”类型,以便在使用返回的数据时在我的代码中使用。即将笔记映射到 React 组件等。
有没有办法缩小这种自动生成的类型,或者以某种方式扩展它,让它看起来像:
export type GetNoteQuery = {
getNote: {
__typename: "Note",
id: string,
createdAt: string | null,
updatedAt: string | null,
title: boolean | null,
content: string | null,
} | null,
Run Code Online (Sandbox Code Playgroud)
getNote您可以使用索引查询来获取耦合的类型,Exclude以摆脱null属性类型中的耦合。然后您可以使用它Omit来摆脱多余的财产。
export type GetNoteQuery = {
getNote: {
__typename: "Note",
id: string,
createdAt: string | null,
updatedAt: string | null,
title: boolean | null,
content: string | null,
} | null
}
type Note = Omit<Exclude<GetNoteQuery['getNote'], null>, '__typename'>
Run Code Online (Sandbox Code Playgroud)
您还可以使用接口来为该类型获取更强的名称:
interface Note extends Omit<Exclude<GetNoteQuery['getNote'], null>, '__typename'> { }
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
431 次 |
| 最近记录: |