Skip to content

undefined & null

Just like in JSON, there’s no undefined in Firestore. If you try to set undefined, both Web SDK and Firebase Admin will throw an error unless you set an option to ignore undefined.

However, avoiding undefined types is not always possible when working with existing types or types coming from 3rd-party. That’s why when you set undefined with Typesaurus, it turns into null:

await db.users.set(userId, { lastName: undefined });
const user = await db.users.get(userId);
user.lastName;
//=> null

That also enables you to set null to a field that can be undefined:

interface User {
firstName: string;
lastName?: string | undefined;
}
await db.users.set(userId, { lastName: null });

Typesaurus transforms all types and unions null with undefined both in write arguments and return values.

However, when consuming data from the database, the functions or components that expect original types might start showing errors:

interface User {
firstName: string;
lastName?: string | undefined;
}
function renderUser(user: User) {
return `${user.firstName} ${user.lastName}`;
}
renderUser(userFromDB);
//=> Type 'null' is not assignable to type 'string | undefined'.(2322)

To fix this, you can use the Nullify helper type:

import type { Typesaurus } from "typesaurus";
function renderUser(user: Typesaurus.Nullify<User>) {
return `${user.firstName} ${user.lastName}`;
}
renderUser(userFromDB);
//=> OK

Read more about Nullify

Or, when possible, add or use null instead of undefined:

interface User {
firstName: string;
// Fine
lastName?: string | undefined | null;
}
interface User {
firstName: string;
// Better!
lastName?: string | null;
}

Read more on designing good schemas