Repository: directus/directus
File: sdk/src/types/output.ts (TypeScript)

Code before:
 * Apply the configured fields query parameter on a many to any relation
 */
export type ApplyManyToAnyFields<
	// input types
	Schema,
	JunctionCollection,
	FieldsList,
	// calculated types
	Junction = UnpackList<JunctionCollection>,
> = Prettify<
	Junction extends object
		? PickRelationalFields<FieldsList> extends never
			? ApplyQueryFields<Schema, Junction, Readonly<UnpackList<FieldsList>>> // no relational fields
			: 'item' extends keyof PickRelationalFields<FieldsList> // do m2a magic
			  ? PickRelationalFields<FieldsList>['item'] extends infer ItemFields
					? Omit<ApplyQueryFields<Schema, Omit<Junction, 'item'>, Readonly<UnpackList<FieldsList>>>, 'item'> & {
							item: {
								[Scope in keyof ItemFields]: Scope extends keyof Schema
									? ApplyNestedQueryFields<Schema, Schema[Scope], ItemFields[Scope]>
									: never;
							}[keyof ItemFields];
					  }
					: never
			  : ApplyQueryFields<Schema, Junction, Readonly<UnpackList<FieldsList>>> // no items query
		: never
>;

/**
 * wrapper to aid in recursion
 */
export type ApplyNestedQueryFields<Schema, Collection, Fields> = Collection extends object
	? ApplyQueryFields<Schema, Collection, Readonly<UnpackList<Fields>>>
	: never;

/**
 * Carry nullability of
 */
export type RelationNullable<Relation, Output> = IsNullable<Relation, Output | null, Output>;

/**
 * Map literal types to actual output types
 */
export type MapFlatFields<
	Item extends object,
	Fields extends keyof Item,
	FunctionMap extends Record<string, string>,
> = {
	[F in Fields as F extends keyof FunctionMap ? FunctionMap[F] : F]: F extends keyof FunctionMap
		? FunctionOutputType
		: Extract<Item[F], keyof FieldOutputMap> extends infer A
		  ? A[] extends never[]

Review comment (suggestion):
Not required for this type as it's only used in the above `ApplyQueryFields` type which is already prettified:

```suggestion
> = Junction extends object
	? PickRelationalFields<FieldsList> extends never
		? ApplyQueryFields<Schema, Junction, Readonly<UnpackList<FieldsList>>> // no relational fields
		: 'item' extends keyof PickRelationalFields<FieldsList> // do m2a magic
		  ? PickRelationalFields<FieldsList>['item'] extends infer ItemFields
				? Omit<ApplyQueryFields<Schema, Omit<Junction, 'item'>, Readonly<UnpackList<FieldsList>>>, 'item'> & {
						item: {
							[Scope in keyof ItemFields]: Scope extends keyof Schema
								? ApplyNestedQueryFields<Schema, Schema[Scope], ItemFields[Scope]>
								: never;
						}[keyof ItemFields];
				  }
				: never
		  : ApplyQueryFields<Schema, Junction, Readonly<UnpackList<FieldsList>>> // no items query
	: never;
```

Code after:
 * Apply the configured fields query parameter on a many to any relation
 */
export type ApplyManyToAnyFields<
	// input types
	Schema,
	JunctionCollection,
	FieldsList,
	// calculated types
	Junction = UnpackList<JunctionCollection>,
> = Junction extends object
	? PickRelationalFields<FieldsList> extends never
		? ApplyQueryFields<Schema, Junction, Readonly<UnpackList<FieldsList>>> // no relational fields
		: 'item' extends keyof PickRelationalFields<FieldsList> // do m2a magic
		  ? PickRelationalFields<FieldsList>['item'] extends infer ItemFields
				? Omit<ApplyQueryFields<Schema, Omit<Junction, 'item'>, Readonly<UnpackList<FieldsList>>>, 'item'> & {
						item: {
							[Scope in keyof ItemFields]: Scope extends keyof Schema
								? ApplyNestedQueryFields<Schema, Schema[Scope], ItemFields[Scope]>
								: never;
						}[keyof ItemFields];
				  }
				: never
		  : ApplyQueryFields<Schema, Junction, Readonly<UnpackList<FieldsList>>> // no items query
	: never;

/**
 * wrapper to aid in recursion
 */
export type ApplyNestedQueryFields<Schema, Collection, Fields> = Collection extends object
	? ApplyQueryFields<Schema, Collection, Readonly<UnpackList<Fields>>>
	: never;

/**
 * Carry nullability of
 */
export type RelationNullable<Relation, Output> = IsNullable<Relation, Output | null, Output>;

/**
 * Map literal types to actual output types
 */
export type MapFlatFields<
	Item extends object,
	Fields extends keyof Item,
	FunctionMap extends Record<string, string>,
> = {
	[F in Fields as F extends keyof FunctionMap ? FunctionMap[F] : F]: F extends keyof FunctionMap
		? FunctionOutputType
		: Extract<Item[F], keyof FieldOutputMap> extends infer A
		  ? A[] extends never[]
				? Item[F]
				: A extends keyof FieldOutputMap