Handle repeatable components and implement a better ay to get array values into an union type

This commit is contained in:
Convly 2022-06-03 20:29:46 +02:00
parent 1855ea73bf
commit 1a9c9ffd6b
4 changed files with 23 additions and 19 deletions

View File

@ -1,13 +1,23 @@
import { Attribute } from './base';
import { GetAttributesValues } from './utils';
export interface ComponentAttribute<T extends Strapi.ComponentUIDs> extends Attribute<'component'> {
export interface ComponentAttribute<T extends Strapi.ComponentUIDs, R extends boolean = false>
extends Attribute<'component'> {
component: T;
repeatable?: boolean;
repeatable?: R;
}
export type ComponentValue<T extends Strapi.ComponentUIDs> = GetAttributesValues<T>;
export type GetComponentAttributeValue<T extends Attribute> = T extends ComponentAttribute<infer U>
? ComponentValue<U>
export type ComponentValue<T extends Strapi.ComponentUIDs, R extends boolean> = GetAttributesValues<
T
> extends infer V
? R extends true
? V[]
: V
: never;
export type GetComponentAttributeValue<T extends Attribute> = T extends ComponentAttribute<
infer U,
infer R
>
? ComponentValue<U, R>
: never;

View File

@ -1,4 +1,4 @@
import { SchemaUID, ValuesOf } from '../../utils';
import { SchemaUID, GetArrayValues } from '../../utils';
import { Attribute } from './base';
import { GetAttributesValues } from './utils';
@ -8,7 +8,7 @@ export interface DynamicZoneAttribute<T extends Strapi.ComponentUIDs[] = []>
}
type DynamicZoneValue<T extends Strapi.ComponentUIDs[]> = Array<
ValuesOf<T> extends infer P
GetArrayValues<T> extends infer P
? P extends SchemaUID
? GetAttributesValues<P> & { __component: P }
: never

View File

@ -1,11 +1,11 @@
import { ValuesOf } from '../../utils';
import { GetArrayValues } from '../../utils';
import { Attribute } from './base';
export interface EnumerationAttribute<T extends string[] = []> extends Attribute<'enumeration'> {
enum: T;
}
export type EnumerationValue<T extends string[]> = ValuesOf<T>;
export type EnumerationValue<T extends string[]> = GetArrayValues<T>;
export type GetEnumerationAttributeValue<T extends Attribute> = T extends EnumerationAttribute<
infer U

View File

@ -6,16 +6,10 @@
/**
*
* Extract the given type's values in an union type
* Extract the array values into an union type
*
* @example
* type X = ValuesOf<{ foo: string, bar: number }>
* // string | number
*
* type X = ValuesOf<['foo', 'bar']>
* // 'foo' | 'bar'
*/
export type ValuesOf<T> = T[keyof T];
**/
export type GetArrayValues<T extends Array<unknown>> = T extends Array<infer U> ? U : never;
/**
* Creates a record where every key is a string and every value is `T`