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

Code before:
	FlatFields extends keyof CollectionItem = FieldsWildcard<CollectionItem, Exclude<Fields, RelationalKeys>>,
> = Prettify<
	IfAny<
		Schema,
		Record<string, any>,
		Merge<
			MappedFunctionFields<Schema, CollectionItem> extends infer FF
				? MapFlatFields<CollectionItem, FlatFields, FF extends Record<string, string> ? FF : Record<string, string>>
				: never,
			RelationalFields extends never
				? never
				: {
						[Field in keyof RelationalFields]: Field extends keyof CollectionItem
							? Extract<CollectionItem[Field], ItemType<Schema>> extends infer RelatedCollection
								? RelationNullable<
										CollectionItem[Field],
										RelatedCollection extends any[]
											? HasManyToAnyRelation<RelatedCollection> extends never
												? ApplyNestedQueryFields<Schema, RelatedCollection, RelationalFields[Field]>[] | null // many-to-many or one-to-many
												: ApplyManyToAnyFields<Schema, RelatedCollection, RelationalFields[Field]>[] // many-to-any'
											: ApplyNestedQueryFields<Schema, RelatedCollection, RelationalFields[Field]> // many-to-one
								  >
								: never
							: never;
				  }
		>
	>
>;

/**
 * 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];

Review comment (refactor):
Small optimization by only applying it around `Merge` (and not in the "is any" case):

```suggestion
> = IfAny<
	Schema,
	Record<string, any>,
	Prettify<
		Merge<
			MappedFunctionFields<Schema, CollectionItem> extends infer FF
				? MapFlatFields<CollectionItem, FlatFields, FF extends Record<string, string> ? FF : Record<string, string>>
				: never,
			RelationalFields extends never
				? never
				: {
						[Field in keyof RelationalFields]: Field extends keyof CollectionItem
							? Extract<CollectionItem[Field], ItemType<Schema>> extends infer RelatedCollection
								? RelationNullable<
										CollectionItem[Field],
										RelatedCollection extends any[]
											? HasManyToAnyRelation<RelatedCollection> extends never
												? ApplyNestedQueryFields<Schema, RelatedCollection, RelationalFields[Field]>[] | null // many-to-many or one-to-many
												: ApplyManyToAnyFields<Schema, RelatedCollection, RelationalFields[Field]>[] // many-to-any'
											: ApplyNestedQueryFields<Schema, RelatedCollection, RelationalFields[Field]> // many-to-one
								  >
								: never
							: never;
				  }
		>
```

Code after:
	FlatFields extends keyof CollectionItem = FieldsWildcard<CollectionItem, Exclude<Fields, RelationalKeys>>,
> = IfAny<
	Schema,
	Record<string, any>,
	Prettify<
		Merge<
			MappedFunctionFields<Schema, CollectionItem> extends infer FF
				? MapFlatFields<CollectionItem, FlatFields, FF extends Record<string, string> ? FF : Record<string, string>>
				: never,
			RelationalFields extends never
				? never
				: {
						[Field in keyof RelationalFields]: Field extends keyof CollectionItem
							? Extract<CollectionItem[Field], ItemType<Schema>> extends infer RelatedCollection
								? RelationNullable<
										CollectionItem[Field],
										RelatedCollection extends any[]
											? HasManyToAnyRelation<RelatedCollection> extends never
												? ApplyNestedQueryFields<Schema, RelatedCollection, RelationalFields[Field]>[] | null // many-to-many or one-to-many
												: ApplyManyToAnyFields<Schema, RelatedCollection, RelationalFields[Field]>[] // many-to-any'
											: ApplyNestedQueryFields<Schema, RelatedCollection, RelationalFields[Field]> // many-to-one
								  >
								: never
							: never;
				  }
		>
	>
>;

/**
 * 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];
				  }