Files
react-native/Libraries/Storage/AsyncStorage.d.ts
T
Nick Gerleman 8cdc9e7f04 Place TypeScript Declarations Alongside Source Files
Summary:
React Native's TS definitions are currently mostly stored in one monolithic file. This change splits the definitions up to correspond to the source files they came from, and are placed next to the source files. I think this should help inform, and make it easy to update the TS declarations when touching the Flow file.

I noticed as part of the change that the typings have not yet removed many APIs that were removed from RN. This is bad, since it means using the removed/non-functional API doesn't cause typechecker errors. Locating typings next to source should prevent that from being able to happen.

The organization here means individual TS declarations can declare what will be in the RN entrypoint, which is a little confusing. Seems like a good potential next refactor, beyond the literal translation I did.

Changelog:
[General][Changed] - Place TS Declarations Alongside Source Files

Reviewed By: lunaleaps, rshest

Differential Revision: D39796598

fbshipit-source-id: b36366466fd1976bdd2d4c8f7a4104a33c457a07
2022-09-26 12:09:45 -07:00

121 lines
3.7 KiB
TypeScript

/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
/**
* AsyncStorage is a simple, unencrypted, asynchronous, persistent, key-value storage
* system that is global to the app. It should be used instead of LocalStorage.
*
* It is recommended that you use an abstraction on top of `AsyncStorage`
* instead of `AsyncStorage` directly for anything more than light usage since
* it operates globally.
*
* On iOS, `AsyncStorage` is backed by native code that stores small values in a
* serialized dictionary and larger values in separate files. On Android,
* `AsyncStorage` will use either [RocksDB](http://rocksdb.org/) or SQLite
* based on what is available.
*
* @see https://reactnative.dev/docs/asyncstorage#content
*/
export interface AsyncStorageStatic {
/**
* Fetches key and passes the result to callback, along with an Error if there is any.
*/
getItem(
key: string,
callback?: (error?: Error, result?: string) => void,
): Promise<string | null>;
/**
* Sets value for key and calls callback on completion, along with an Error if there is any
*/
setItem(
key: string,
value: string,
callback?: (error?: Error) => void,
): Promise<void>;
removeItem(key: string, callback?: (error?: Error) => void): Promise<void>;
/**
* Merges existing value with input value, assuming they are stringified json. Returns a Promise object.
* Not supported by all native implementation
*/
mergeItem(
key: string,
value: string,
callback?: (error?: Error) => void,
): Promise<void>;
/**
* Erases all AsyncStorage for all clients, libraries, etc. You probably don't want to call this.
* Use removeItem or multiRemove to clear only your own keys instead.
*/
clear(callback?: (error?: Error) => void): Promise<void>;
/**
* Gets all keys known to the app, for all callers, libraries, etc
*/
getAllKeys(
callback?: (error?: Error, keys?: string[]) => void,
): Promise<string[]>;
/**
* multiGet invokes callback with an array of key-value pair arrays that matches the input format of multiSet
*/
multiGet(
keys: string[],
callback?: (errors?: Error[], result?: [string, string][]) => void,
): Promise<[string, string][]>;
/**
* multiSet and multiMerge take arrays of key-value array pairs that match the output of multiGet,
*
* multiSet([['k1', 'val1'], ['k2', 'val2']], cb);
*/
multiSet(
keyValuePairs: string[][],
callback?: (errors?: Error[]) => void,
): Promise<void>;
/**
* Delete all the keys in the keys array.
*/
multiRemove(
keys: string[],
callback?: (errors?: Error[]) => void,
): Promise<void>;
/**
* Merges existing values with input values, assuming they are stringified json.
* Returns a Promise object.
*
* Not supported by all native implementations.
*/
multiMerge(
keyValuePairs: string[][],
callback?: (errors?: Error[]) => void,
): Promise<void>;
}
/**
* AsyncStorage has been extracted from react-native core and will be removed in a future release.
* It can now be installed and imported from `@react-native-community/async-storage` instead of 'react-native'.
* @see https://github.com/react-native-community/async-storage
* @deprecated
*/
export const AsyncStorage: AsyncStorageStatic;
/**
* AsyncStorage has been extracted from react-native core and will be removed in a future release.
* It can now be installed and imported from `@react-native-community/async-storage` instead of 'react-native'.
* @see https://github.com/react-native-community/async-storage
* @deprecated
*/
export type AsyncStorage = AsyncStorageStatic;