Skip to content

Commit

Permalink
fix typings for PartialModelGraph and nullable fields
Browse files Browse the repository at this point in the history
Handle `| null` unions properly. (e.g. for nullable `BelongsToOneRelations`).

Before `PartialModelGraph` was converting `field: Model | null;` to `field?: Expression<Model | null>`, and typescript throws errors for graphs with partial sub-graphs for those nullable relations. (expecting full instance of the model class)
Now it converts it properly to `field?: PartialModelGraph<Model> | null` 🎉
  • Loading branch information
falkenhawk committed Apr 22, 2023
1 parent 123efe2 commit 69a172c
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions typings/objection/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,16 +225,20 @@ declare namespace Objection {
*/
type PartialModelGraph<M, T = M & GraphParameters> = T extends any
? {
[K in DataPropertyNames<T>]?: Defined<T[K]> extends Model
? PartialModelGraph<Defined<T[K]>>
: Defined<T[K]> extends Array<infer I>
? I extends Model
? PartialModelGraph<I>[]
: Expression<T[K]>
: Expression<T[K]>;
[K in DataPropertyNames<T>]?: null extends T[K]
? PartialModelGraphField<NonNullable<T[K]>> | null // handle nullable BelongsToOneRelations
: PartialModelGraphField<T[K]>;
}
: never;

type PartialModelGraphField<F> = Defined<F> extends Model
? PartialModelGraph<Defined<F>>
: Defined<F> extends Array<infer I>
? I extends Model
? PartialModelGraph<I>[]
: Expression<F>
: Expression<F>;

/**
* Extracts the property names (excluding relations) of a model class.
*/
Expand Down

0 comments on commit 69a172c

Please # to comment.