Open itsramiel opened 3 months ago
Hi @itsramiel , I had the same issue here and this is how I resolved it. I don't love that I'm ignoring TS errors and using a type assertion, but it does the job for me.
import {
Model, Relation as WatermelonDbRelation,
} from '@nozbe/watermelondb';
import {
Observable,
} from 'rxjs';
/**
* Wrapper of `Relation` to correctly type optional relations. These are
* relations where the fk is an optional field.
*/
export default class OptionalRelation<
T extends Model,
> extends WatermelonDbRelation<T> {
// @ts-ignore
fetch() {
return super.fetch() as Promise<T | null>;
}
// @ts-ignore
observe() {
return super.observe() as Observable<T | null>;
}
}
@relation(TableName.BLOGS, 'blog_id') blog!: OptionalRelation<Blog>;
Hey,
I have a use case where a relationship can be nullable. I defined this in the schema using the
isOptional
but how would I go about typing the field in the model?Given the models in the example of this repo:
Let's assume for the sake of example that you post a
Post
under no specificBlog
how would:@relation(TableName.BLOGS, 'blog_id') blog!: Relation<Blog>;
change?Changing the type to
Relation<Blog> | null
doesnt work correctly becausepost.blog
always return an object even the blog is nullable. AndRelation<Blog | null>
doesnt work because the generic thatRelation
takes extendsModel
which is not nullable.How do I go about creating nullable relations with type safety?