Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6287

How to declare types for optional fields in realm objects in typescript?

$
0
0

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)}
  1. If sku is undefined (the value or field doesn't exists in item), the value will not be updated.
  2. 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?


Viewing all articles
Browse latest Browse all 6287

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>