Skip to content

Commit b05d92f

Browse files
authored
Merge pull request #16 from ovos/fix-typings-for-graphs-with-nullable-fields
fix typings for PartialModelGraph and nullable fields
2 parents 06c9746 + 479a03d commit b05d92f

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

typings/objection/index.d.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -222,14 +222,18 @@ declare namespace Objection {
222222
* Just like PartialModelObject but this is applied recursively to relations.
223223
*/
224224
type PartialModelGraph<M, T = M & GraphParameters> = {
225-
[K in DataPropertyNames<T>]?: Defined<T[K]> extends Model
226-
? PartialModelGraph<Defined<T[K]>>
227-
: Defined<T[K]> extends Array<infer I>
225+
[K in DataPropertyNames<T>]?: null extends T[K]
226+
? PartialModelGraphField<NonNullable<T[K]>> | null // handle nullable BelongsToOneRelations
227+
: PartialModelGraphField<T[K]>;
228+
};
229+
230+
type PartialModelGraphField<F> = Defined<F> extends Model
231+
? PartialModelGraph<Defined<F>>
232+
: Defined<F> extends Array<infer I>
228233
? I extends Model
229234
? PartialModelGraph<I>[]
230-
: Expression<T[K]>
231-
: Expression<T[K]>;
232-
};
235+
: Expression<F>
236+
: Expression<F>;
233237

234238
/**
235239
* Extracts the property names (excluding relations) of a model class.

0 commit comments

Comments
 (0)