Skip to content

Commit 378a6b5

Browse files
authored
fix: enum array filter typing (#200)
1 parent 5ff4ad0 commit 378a6b5

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

packages/runtime/src/client/crud-types.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ export type WhereInput<
218218
? // relation
219219
RelationFilter<Schema, Model, Key>
220220
: FieldIsArray<Schema, Model, Key> extends true
221-
? ArrayFilter<GetModelFieldType<Schema, Model, Key>>
221+
? ArrayFilter<Schema, GetModelFieldType<Schema, Model, Key>>
222222
: // enum
223223
GetModelFieldType<Schema, Model, Key> extends GetEnums<Schema>
224224
? EnumFilter<Schema, GetModelFieldType<Schema, Model, Key>, ModelFieldIsOptional<Schema, Model, Key>>
@@ -246,14 +246,18 @@ type EnumFilter<Schema extends SchemaDef, T extends GetEnums<Schema>, Nullable e
246246
not?: EnumFilter<Schema, T, Nullable>;
247247
};
248248

249-
type ArrayFilter<T extends string> = {
250-
equals?: MapBaseType<T>[];
251-
has?: MapBaseType<T>;
252-
hasEvery?: MapBaseType<T>[];
253-
hasSome?: MapBaseType<T>[];
249+
type ArrayFilter<Schema extends SchemaDef, T extends string> = {
250+
equals?: MapScalarType<Schema, T>[];
251+
has?: MapScalarType<Schema, T>;
252+
hasEvery?: MapScalarType<Schema, T>[];
253+
hasSome?: MapScalarType<Schema, T>[];
254254
isEmpty?: boolean;
255255
};
256256

257+
// map a scalar type (primitive and enum) to TS type
258+
type MapScalarType<Schema extends SchemaDef, T extends string> =
259+
T extends GetEnums<Schema> ? keyof GetEnum<Schema, T> : MapBaseType<T>;
260+
257261
type PrimitiveFilter<
258262
Schema extends SchemaDef,
259263
T extends string,

packages/runtime/test/schemas/typing/typecheck.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,17 @@ async function find() {
8080
where: { name: 'Alex' },
8181
});
8282

83+
// enum array
84+
await client.user.findFirst({
85+
where: { status: { equals: [Status.ACTIVE] } },
86+
});
87+
await client.user.findFirst({
88+
where: { status: { has: Status.ACTIVE } },
89+
});
90+
await client.user.findFirst({
91+
where: { status: { hasEvery: [Status.ACTIVE] } },
92+
});
93+
8394
await client.user.findMany({
8495
skip: 1,
8596
take: 1,

0 commit comments

Comments
 (0)