Is there a way to force all elements in array?
Only requirement is "type connection" to main Book type.
I hope there is some way to make tuple type ['id', 'title'] from Book type generically, as suggested in comments. Or at least array length check...
type Book = {
id: string
title: string
visible: boolean
author: string
}
type BookFields = keyof Book
type BookFieldsExtract<ExtractedFields extends BookFields> = Extract<BookFields, ExtractedFields>
const arr: BookFieldsExtract<'id' | 'title'>[] = ['id'] // how to force error if title not inclided in array?
Use case:
type SelectedBookFields = BookFieldsExtract<'id' | 'title'>
type ApiRes = ApiResBook<SelectedBookFields> // {book: {id: string, title: string}}
const bookFields: SelectedBookFields[] = ['id', 'title'] // i just don't want to forget some fields when making request
const response = await fetchAPI<ApiReqBookBody, ApiRes>(
'book',
{ fields: { book: bookFields } }
) // response type is {error: true} | {book: {id: string, title: string}}
['id', 'title'] | ['title', 'id']