Skip to content

TypeScript error after updating from EXPO SDK 52 to 53 #812

@CzepiM200

Description

@CzepiM200

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch react-native-bouncy-checkbox@4.1.2 for the project I'm working on.

Issue after updating from EXPO SDK 52 to 53:

yarn run v1.22.22
$ tsc
node_modules/react-native-bouncy-checkbox/lib/helpers/useStateWithCallback.ts:17:23 - error TS2554: Expected 1 arguments, but got 0.
17 const callbackRef = useRef<Callback>();
~~~~~~
node_modules/@types/react/index.d.ts:1726:24
1726 function useRef(initialValue: T): RefObject;
~~~~~~~~~~~~~~~
An argument for 'initialValue' was not provided.
node_modules/react-native-bouncy-checkbox/lib/helpers/useStateWithCallback.ts:22:7 - error TS2322: Type 'Callback | undefined' is not assignable to type 'Callback'.
Type 'undefined' is not assignable to type 'Callback'.
22 callbackRef.current = callback;

Here is the diff that solved my problem:

diff --git a/node_modules/react-native-bouncy-checkbox/lib/BouncyCheckbox.tsx b/node_modules/react-native-bouncy-checkbox/lib/BouncyCheckbox.tsx
index 7902d02..13505d9 100644
--- a/node_modules/react-native-bouncy-checkbox/lib/BouncyCheckbox.tsx
+++ b/node_modules/react-native-bouncy-checkbox/lib/BouncyCheckbox.tsx
@@ -78,7 +78,7 @@ const BouncyCheckbox: React.ForwardRefRenderFunction<
         bounceVelocityOut,
         bouncinessOut,
       );
-      onPress && onPress(newCheckedValue);
+      onPress && onPress(newCheckedValue ?? false);
     });
   }, [
     useBuiltInState,
@@ -104,7 +104,7 @@ const BouncyCheckbox: React.ForwardRefRenderFunction<
     }
 
     setChecked(!checked, (newCheckedValue) => {
-      onLongPress && onLongPress(newCheckedValue);
+      onLongPress && onLongPress(newCheckedValue ?? false);
     });
   }, [checked, onLongPress, setChecked, useBuiltInState]);
 
diff --git a/node_modules/react-native-bouncy-checkbox/lib/helpers/useStateWithCallback.ts b/node_modules/react-native-bouncy-checkbox/lib/helpers/useStateWithCallback.ts
index 42aa2d3..2ae2bee 100644
--- a/node_modules/react-native-bouncy-checkbox/lib/helpers/useStateWithCallback.ts
+++ b/node_modules/react-native-bouncy-checkbox/lib/helpers/useStateWithCallback.ts
@@ -1,39 +1,33 @@
-import {
-  useRef,
-  useState,
-  useEffect,
-  useCallback,
-  SetStateAction,
-} from "react";
+import { SetStateAction, useCallback, useEffect, useRef, useState } from 'react'
 
-type Callback<T> = (value?: any) => void;
-type DispatchWithCallback<T> = (value: any, callback?: Callback<any>) => void;
+type Callback<T> = (value?: T) => void;
+type DispatchWithCallback<T> = (value: SetStateAction<T>, callback?: Callback<T>) => void;
 
 function useStateWithCallback<T>(
-  initialState: any | (() => any),
-): [any, DispatchWithCallback<SetStateAction<any>>] {
-  const [state, _setState] = useState(initialState);
+  initialState: T | (() => T),
+): [T, DispatchWithCallback<T>] {
+  const [state, _setState] = useState<T>(initialState)
 
-  const callbackRef = useRef<Callback<any>>();
-  const isFirstCallbackCall = useRef<boolean>(true);
+  const callbackRef = useRef<Callback<T> | null>(null) // ✅ fix
+  const isFirstCallbackCall = useRef<boolean>(true)
 
-  const setState = useCallback(
-    (setStateAction: SetStateAction<any>, callback?: Callback<any>): void => {
-      callbackRef.current = callback;
-      _setState(setStateAction);
+  const setState: DispatchWithCallback<T> = useCallback(
+    (setStateAction, callback) => {
+      callbackRef.current = callback ?? null
+      _setState(setStateAction)
     },
     [],
-  );
+  )
 
   useEffect(() => {
     if (isFirstCallbackCall.current) {
-      isFirstCallbackCall.current = false;
-      return;
+      isFirstCallbackCall.current = false
+      return
     }
-    callbackRef.current?.(state);
-  }, [state]);
+    callbackRef.current?.(state)
+  }, [state])
 
-  return [state, setState];
+  return [state, setState]
 }
 
-export default useStateWithCallback;
+export default useStateWithCallback

This issue body was partially generated by patch-package.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions