Files
apollo/packages/vue-apollo-composable/src/util/ExtractSingleKey.ts
T
Caleb Cox 619b0d1e62 types: Single key result with __typename still counts as single key (#1163)
Previously, a response with a single key and a __typename field would
not be considered as having a single key and could not be
automatically extracted by useQuery.
2021-07-04 23:42:21 +02:00

10 lines
435 B
TypeScript

/**
* Check if a type is a union, and return true if so, otherwise false.
*/
export type IsUnion<T, U = T> = U extends any ? ([T] extends [U] ? false : true) : never
/**
* Extracts an inner type if T has a single key K, otherwise it returns T.
*/
export type ExtractSingleKey<T, K extends keyof T = keyof T, KWithoutTypename extends K = Exclude<K, '__typename'>> = IsUnion<KWithoutTypename> extends true ? T : T[KWithoutTypename]