question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Problem using with react-native and Typescript

See original GitHub issue

Describe the bug

I’m trying to use with react-native and typescript. I have a simple one field form like this:

const { register, handleSubmit, setValue, errors } = useForm();
return (
  <SafeAreaView>
    <Input {/* from react native elements */}
      keyboardType="phone-pad"
      placeholder="Phone"
      ref={register({ name: 'phone' }, { required: true })}
      onChangeText={text => setValue('phone', text)}
    />
    <Button {/* from react native elements */}
      title="Signup"
      onPress={handleSubmit(onSubmit)}
    />
  </SafeAreaView>
)

This code works. But when I typecheck the code, I get two errors.

First:

src/screens/Phone.tsx:31:9 - error TS2322: Type 'void' is not assignable to type 'string | ((instance: Input | null) => void) | RefObject<Input> | null | undefined'.

31         ref={register({ name: 'phone' }, {required: true})}
           ~~~

  node_modules/@types/react/index.d.ts:95:9
    95         ref?: LegacyRef<T>;
               ~~~
    The expected type comes from property 'ref' which is declared here on type 'IntrinsicAttributes & IntrinsicClassAttributes<Input> & Readonly<InputProps> & Readonly<{ children?: ReactNode; }>'

I guess I can solve this by just putting the register call outside the jsx, i.e not passing anything as ref to the Input element. Would that be the best approach?

Second:

src/screens/Phone.tsx:38:9 - error TS2322: Type '(e: SyntheticEvent<Element, Event>) => Promise<void>' is not assignable to type '(event: GestureResponderEvent) => void'.
  Types of parameters 'e' and 'event' are incompatible.
    Type 'GestureResponderEvent' is not assignable to type 'SyntheticEvent<Element, Event>'.
      Types of property 'nativeEvent' are incompatible.
        Type 'NativeTouchEvent' is not assignable to type 'Event'.
          Property 'bubbles' is missing in type 'NativeTouchEvent'.

38         onPress={handleSubmit(onSubmit)}
           ~~~~~~~

  node_modules/@types/react-native/index.d.ts:4768:5
    4768     onPress?: (event: GestureResponderEvent) => void;
             ~~~~~~~
    The expected type comes from property 'onPress' which is declared here on type 'IntrinsicAttributes & IntrinsicClassAttributes<Button> & Readonly<ButtonProps> & Readonly<{ children?: ReactNode; }>'

This is because of the event type. How can I mute this?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:22 (11 by maintainers)

github_iconTop GitHub Comments

3reactions
bluebill1049commented, Oct 7, 2019

you want to try use useEffect to register input instead?

useEffect(() => {
  register({ name: 'phone' }, { required: true })
}, [register])
2reactions
bluebill1049commented, Dec 22, 2019

thanks @AdhamMoussa for the update.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using TypeScript - React Native
TypeScript is a language which extends JavaScript by adding type definitions, much like Flow. ... To fix the issue try uninstalling the CLI:....
Read more >
Errors when configuring react native with typescript
It looks like the React Native type declarations conflict with the browser DOM declarations that TypeScript loads by default.
Read more >
Should You Use TypeScript with React Native? [2021]
Cons of TypeScript in React Native · Another dependency to manage - If you add a dependency you have to manage it, understand...
Read more >
Should we use TypeScript with React Native ? - GeeksforGeeks
Before going into the question of whether should you use typescript with React Native or not, let us take a look at typescript...
Read more >
Using TypeScript with React Native - LogRocket Blog
An introduction to using TypeScript in React Native apps, including a tutorial and example build for a mobile app.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found