Let's say I have the following definition in the schema:
export const ItemSchema = { name: 'item', properties: { _id: 'objectId', sku: 'string?', name: 'string', updateDatetime: 'date', creationDatetime: 'date', }, primaryKey: '_id',};
In this case the field sku is optional. How I should declare the field in the interface using typescript?
I have three options right now.
Having sku as nullable:
export interface Item { _id: ObjectId; sku: string | null; name: string; updateDatetime: Date; creationDatetime: Date;}
Having sku as optional:
export interface Item { _id: ObjectId; sku?: string; name: string; updateDatetime: Date; creationDatetime: Date;}
Having sku as optional and nullable:
export interface Item { _id: ObjectId; sku?: string | null; name: string; updateDatetime: Date; creationDatetime: Date;}
From my side any way of doing it is basically the same but because I always do upserts the values doesn't mean the same. For this code:
realm.write(() => { realm.create<Item>('item', item, UpdateMode.Modified)}
- If sku is undefined (the value or field doesn't exists in item), the value will not be updated.
- If sku is null the value will be set to null.
So, what should be the correct way of declaring the type to handle it in my application?