typescript - Typescript Pick Array 未按预期工作
问题描述
如果我将此行用作数组以及如何修复它,我不确定为什么会出现问题:
Pick<Author, PickedAuthorFields>[]
沙盒。
如果我使用 never 代替条件类型,那么我在必填作者字段方面还有另一个问题。
沙盒 3 可选?作者还有一个问题...
许多页面需要此 ApiResBook 泛型类型,这些页面将根据请求向具有不同预期输出字段的 api 发出请求。
也许有替代方法,如果需要,我可以改变对象的形状。
// Main types as in database - should't be changed
type Book = {
id: string
title: string
visible: boolean
author: string
}
type Author = {
id: string
name: string
}
// Inhereted from types from main
type BookFields = keyof Book
type AuthorFields = keyof Author
// type for generating expected fetch response from API
type ApiResBook<
PickedBookFields extends BookFields,
PickedAuthorFields extends AuthorFields | undefined = undefined,
> = {
book: Pick<Book, PickedBookFields> & {
author: PickedAuthorFields extends AuthorFields ? Pick<Author, PickedAuthorFields>[] : undefined
}
}
// tests
type BookWithAuthor = ApiResBook<'id', 'name' | 'id'>
// should be ok
const bookWithAuthor1: BookWithAuthor = { book: { id: '1', author: [{ id: '1' }] } }
const bookWithAuthor2: BookWithAuthor = { book: { id: '1', author: [{ name: 'Max' }] } }
const bookWithAuthor3: BookWithAuthor = { book: { id: '1', author: [{ name: 'Max', id: '1' }] } } // why error?
解决方案
需要一段时间才能弄清楚,但我认为这是解决方案:
type Book = {
id: string
title: string
visible: boolean
author: string
}
type Author = {
id: string
name: string
}
// Inhereted from types from main
type BookFields = keyof Book
type AuthorFields = keyof Author
// type for generating expected fetch response from API
type ApiResBook<
PickedBookFields extends BookFields,
PickedAuthorFields extends AuthorFields | null = null,
AuthorObj = {author: Pick<Author, Exclude<PickedAuthorFields, null>>[]}
> = {
book: Pick<Book, PickedBookFields> & (PickedAuthorFields extends null ? {} : AuthorObj)
}
// tests
type BookWithAuthor = ApiResBook<'id', 'name' | 'id'>
type BookWithoutAuthor = ApiResBook<'id'>
// should be ok
const bookWithAuthor0: BookWithoutAuthor = { book: { id: '1' } }
const bookWithAuthor3: BookWithAuthor = { book: { id: '1', author: [{ id: '1', name: 'Max' }] } }
PS。null 可以替换为 undefined 或任何其他单位类型
推荐阅读
- c++ - 程序永远不会像我期望的那样退出的双线程问题
- linux - 使用 Bash 的简单“Hello World”错误
- migration - 当尝试使用帖子创建新的迁移表时,它显示以下错误
- c++ - While循环何时使用OR或AND?
- google-apps-script - 更改时自动更新的自定义谷歌表格功能不会在其他人编辑时更改
- python - SQLAlchemy 过滤器 - 全选?
- node.js - 当路由器自行安装时会发生什么?
- reactjs - React-admin:ReferenceInput 不显示加载
在加载可能的选项时 - python - 保存 Abjad 输出 pdf
- pandas - 为什么我不能将列合并在一起