我想指定一个动态对象键只能是联合的1个值:
export enum HTTPMethod {
DELETE = 'DELETE',
GET = 'GET',
HEAD = 'HEAD',
OTIONS = 'OPTIONS',
PATCH = 'PATCH',
POST = 'POST',
PUT = 'PUT',
}
export type FetchData<TData> = (
routeOrBody?: string | BodyInit | object,
body?: BodyInit | object,
) => Promise<TData | undefined>
export type GraphqlFetch<TData> = (
query: string,
variables?: BodyInit | object,
) => Promise<TData>
export type AbortFetch = () => void
export interface FetchCommands<TData = any> {
get: FetchData<TData>
post: FetchData<TData>
patch: FetchData<TData>
put: FetchData<TData>
del: FetchData<TData>
delete: FetchData<TData>
query: GraphqlFetch<TData>
mutate: GraphqlFetch<TData>
abort: AbortFetch
}
export interface UseFetchBaseResult<TData = any> {
data: TData | undefined
loading: boolean
error: Error
}
export type RestFetchMethods =
| 'get'
| 'post'
| 'patch'
| 'put'
| 'del'
| 'delete'
export type GraphqlFetchMethods = 'query' | 'mutate'
export type FetchRequest<TData> = {
[key in keyof FetchCommands<TData>]: key extends RestFetchMethods
? FetchData<TData>
: key extends GraphqlFetchMethods
? GraphqlFetch<TData>
: AbortFetch
}
export type DestructuringCommands<TData> = [
TData | undefined,
FetchCommands<TData>,
]
export type ObjectDestructure<
TKey extends keyof FetchCommands,
TData
> = UseFetchBaseResult<TData> & { [key: TKey]: FetchRequest<TData>[TKey] }
具体来说就是这种类型:
export type ObjectDestructure<
TKey extends keyof FetchCommands,
TData
> = UseFetchBaseResult<TData> & { [key: TKey]: FetchRequest<TData>[TKey] }
我希望Tkey是联合中的1值,而不是映射类型,但是[key: TKey]
出现
索引签名参数类型必须为“字符串”或“数字”。
Here是一个带有代码的游乐场