Skip to content

Merges an existing key value with an input value, assuming both values are stringified JSON (with TypeScript) #826

Closed
@tiavina-mika

Description

@tiavina-mika

What happened?

I'm using TypeScript and I have this error when using mergeItem:
Merges an existing key value with an input value, assuming both values are stringified JSON

Here is my code:

export const mergeAsyncStorage = async (key: string, value: Record<string, any>): Promise<void> => {
  try {
    if (!key || !value) {
      throw new Error('Missing key and data params');
    }

    await AsyncStorage.mergeItem(key, JSON.stringify(value));
  } catch (e) {
    console.error('setAsyncStorage error:', e);
  }
};

Here is the complet error messages:
`
Merges an existing key value with an input value, assuming both values are stringified JSON.

NOTE: This is not supported by all native implementations.

See https://react-native-async-storage.github.io/async-storage/docs/api#mergeitem

Cannot invoke an object which is possibly 'undefined'.ts(2722)
`

Version

~1.17.3

What platforms are you seeing this issue on?

  • Android
  • iOS
  • macOS
  • Windows
  • web

System Information

System:
    OS: Windows 10 10.0.19043
    CPU: (8) x64 Intel(R) Core(TM) i7-4710HQ CPU @ 2.50GHz
    Memory: 7.00 GB / 15.95 GB
  Binaries:
    Node: 14.18.2 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.18 - C:\Program Files\nodejs\yarn.CMD
    npm: 6.14.15 - C:\Program Files\nodejs\npm.CMD
    Watchman: 20210110.135312.0 - C:\ProgramData\chocolatey\bin\watchman.EXE
  SDKs:
    Android SDK:
      API Levels: 28, 29, 30, 31, 32
      Build Tools: 28.0.3, 29.0.2, 29.0.3, 30.0.2, 32.0.0
      System Images: android-28 | Intel x86 Atom_64, android-28 | Google APIs Intel x86 Atom, android-28 | Google APIs Intel x86 Atom_64, android-28 | Google Play Intel x86 Atom_64, android-29 | Intel x86 Atom_64, android-29 | Google APIs Intel x86 Atom, android-29 | Google Play Intel x86 Atom, android-29 | Google Play Intel x86 Atom_64, android-30 | Intel x86 Atom_64, android-30 | Google APIs Intel x86 Atom, android-30 | Google APIs Intel x86 Atom_64, android-30 | Google Play Intel x86 Atom
      Android NDK: Not Found
    Windows SDK: Not Found
  IDEs:
    Android Studio: Version     2020.3.0.0 AI-203.7717.56.2031.7935034
    Visual Studio: Not Found
  Languages:
    Java: 11.0.13 - /c/Program Files/Common Files/Oracle/Java/javapath/javac
  npmPackages:
    @react-native-community/cli: Not Found
    react: 17.0.2 => 17.0.2
    react-native: 0.68.2 => 0.68.2
    react-native-windows: Not Found
  npmGlobalPackages:
    *react-native*: Not Found

Steps to Reproduce

  1. Create an app with Expo managed workflow and TypeScript.
  2. Add this function, the IDE will show the warning :
export const mergeAsyncStorage = async (key: string, value: Record<string, any>): Promise<void> => {
  try {
    if (!key || !value) {
      throw new Error('Missing key and data params');
    }

    await AsyncStorage.mergeItem(key, JSON.stringify(value));
  } catch (e) {
    console.error('setAsyncStorage error:', e);
  }
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions