From 4659d9ce0584e6fd638a7e37363fdc211f07707b Mon Sep 17 00:00:00 2001 From: Bilal Quadri Date: Thu, 13 Jan 2022 20:54:22 -0800 Subject: [PATCH 1/2] feat(types): set void as this type of hook fns This change improves the experience for TypeScript users that have the [`unbound-method`](https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/eslint-plugin/docs/rules/unbound-method.md) rule enabled. Without this change, check failures occur when users attempt to use destructured references to the functions returned in the `useAsyncStorage` hook's envelope object. Example: ```typescript const { getItem, setItem } = useAsyncStorage('someKey') // eslint@typescript-eslint/unbound-method // // Avoid referencing unbound methods which may cause unintentional // scoping of `this`. If your function does not access `this`, you can // annotate it with `this: void`, or consider using an arrow function // instead. ``` --- types/index.d.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/types/index.d.ts b/types/index.d.ts index e2d3820b..3c310d62 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -60,10 +60,10 @@ interface AsyncStorage { } type AsyncStorageHook = { - getItem(callback?: (error?: Error, result?: string) => void): Promise; - setItem(value: string, callback?: (error?: Error) => void): Promise; - mergeItem(value: string, callback?: (error?: Error) => void): Promise; - removeItem(callback?: (error?: Error) => void): Promise; + getItem(this: void, callback?: (error?: Error, result?: string) => void): Promise; + setItem(this: void, value: string, callback?: (error?: Error) => void): Promise; + mergeItem(this: void, value: string, callback?: (error?: Error) => void): Promise; + removeItem(this: void, callback?: (error?: Error) => void): Promise; } declare module '@react-native-async-storage/async-storage' { From 86560c14c45cd55bb708222a40e416cc61701ac5 Mon Sep 17 00:00:00 2001 From: Bilal Quadri Date: Thu, 20 Jan 2022 22:38:24 -0800 Subject: [PATCH 2/2] chore: define hook types with arrow fns This approach achieves the same end result as explicitly declaring the `this` type to be void and is more familiar to readers who know JS. Co-authored-by: Tommy Nguyen <4123478+tido64@users.noreply.github.com> --- types/index.d.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/types/index.d.ts b/types/index.d.ts index 3c310d62..d7b3f26d 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -60,10 +60,10 @@ interface AsyncStorage { } type AsyncStorageHook = { - getItem(this: void, callback?: (error?: Error, result?: string) => void): Promise; - setItem(this: void, value: string, callback?: (error?: Error) => void): Promise; - mergeItem(this: void, value: string, callback?: (error?: Error) => void): Promise; - removeItem(this: void, callback?: (error?: Error) => void): Promise; + getItem: (callback?: (error?: Error, result?: string) => void) => Promise; + setItem: (value: string, callback?: (error?: Error) => void) => Promise; + mergeItem: (value: string, callback?: (error?: Error) => void) => Promise; + removeItem: (callback?: (error?: Error) => void) => Promise; } declare module '@react-native-async-storage/async-storage' {