mirror of
https://github.com/strapi/strapi.git
synced 2025-09-23 15:29:27 +00:00
Handle repeatable components and implement a better ay to get array values into an union type
This commit is contained in:
parent
1855ea73bf
commit
1a9c9ffd6b
@ -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;
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
12
packages/core/strapi/lib/types/utils.d.ts
vendored
12
packages/core/strapi/lib/types/utils.d.ts
vendored
@ -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`
|
||||
|
Loading…
x
Reference in New Issue
Block a user