Search code examples
nearprotocol

Get default value on storaged u64


I'd like to get storaged value, if it not exist, please give me 0

const lastDeposit = storage.get<u64>('lastDeposit', u64(0)) || u64(0);

But the compiler throw

ERROR AS204: Type 'u64' cannot be nullable.

   static get<T>(key: string, defaultValue: T | null = null): T | null {
                                            ~
 in ~lib/near-sdk-core/storage.ts(178,44)

ERROR AS204: Type 'u64' cannot be nullable.

   static get<T>(key: string, defaultValue: T | null = null): T | null {
                                                              ~
 in ~lib/near-sdk-core/storage.ts(178,62)

Solution

  • you have to phrase this in a way that the lastDeposit is never allowed to be null

    this compiled for me with errors

    const lastDeposit: u64 = storage.getSome<u64>('lastDeposit') || 0;