Skip to content

Commit

Permalink
fix(types): remove 'this' binding expectations on hook fn types (#736)
Browse files Browse the repository at this point in the history
* 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.
```

* 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>
  • Loading branch information
bilalq and tido64 committed Jan 24, 2022
1 parent 0cf9db8 commit cf76a4e
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions types/index.d.ts
Expand Up @@ -60,10 +60,10 @@ interface AsyncStorage {
}

type AsyncStorageHook = {
getItem(callback?: (error?: Error, result?: string) => void): Promise<string | null>;
setItem(value: string, callback?: (error?: Error) => void): Promise<void>;
mergeItem(value: string, callback?: (error?: Error) => void): Promise<void>;
removeItem(callback?: (error?: Error) => void): Promise<void>;
getItem: (callback?: (error?: Error, result?: string) => void) => Promise<string | null>;
setItem: (value: string, callback?: (error?: Error) => void) => Promise<void>;
mergeItem: (value: string, callback?: (error?: Error) => void) => Promise<void>;
removeItem: (callback?: (error?: Error) => void) => Promise<void>;
}

declare module '@react-native-async-storage/async-storage' {
Expand Down

0 comments on commit cf76a4e

Please sign in to comment.