Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issues with Typescript + React native #86

Closed
rafaelcorreiapoli opened this issue Dec 28, 2017 · 9 comments · Fixed by #715
Closed

Issues with Typescript + React native #86

rafaelcorreiapoli opened this issue Dec 28, 2017 · 9 comments · Fixed by #715

Comments

@rafaelcorreiapoli
Copy link

Are you submitting a bug report or a feature request?

Feature request

What is the current behavior?

Type errors with react-native + typescript

What is the expected behavior?

No type errors :P

What's your environment?

I'm using react-final-form with react-native

"react-final-form": "^2.1.1",
"final-form": "^3.0.0",
"react": "16.0.0",
"react-native": "0.50.4",

Other information

There are two situations where I have type errors.

1. handleSubmit

The current type for handleSubmit is:

handleSubmit: (event: React.SyntheticEvent<HTMLFormElement>) => void

How can we deal with this in react-native?
onPress events of touchables have this signature:

onPress?: (event: GestureResponderEvent) => void;

2. Field extra props

Sometimes we need to pass some extra props to the underlying component of Field

            <Field
              name='firstName'
              component={TextInputField}
              style={styles.textInputStyle}
            />

In this case, the style prop is the extra one.
The type system complains because the props that Field is expecting are:

export type FieldProps = {
  allowNull?: boolean
  format?: ((value: any, name: string) => any) | null
  parse?: ((value: any, name: string) => any) | null
  name: string
  subscription?: FieldSubscription
  validate?: (value: any, allValues: object) => any
  value?: any
} & RenderableProps<FieldRenderProps>

Maybe we should allow anything here? :\ 👎

@rafaelcorreiapoli rafaelcorreiapoli changed the title Issues Issues with Typescript + React native Dec 28, 2017
@erikras
Copy link
Member

erikras commented Dec 29, 2017

I think #84 handled the first one, right @cosmith?

Is there a way to say, "This stuff, and anything else" in type definitions?

@rafaelcorreiapoli
Copy link
Author

rafaelcorreiapoli commented Dec 30, 2017

Hmm, maybe:

interface IFieldProps {
     bla: bla;
    [x: string]: any ;
}

But I'm wondering if this is a good practice

@erikras
Copy link
Member

erikras commented Dec 30, 2017

I'm largely ignorant of TS, so don't feel like I can make a judgement call on this. First check other React component libs, and if it seems like something people do, I'd be happy to accept a PR with this change.

@geowarin
Copy link
Contributor

Extra props is a duplicate of #79, it's fixed on master already.

@rafaelcorreiapoli I've never used react-native but looking a the definitions, NativeSyntheticEvent does not extend SyntheticEvent, hence the problem that you describe.

Surely other libraries (compatible with both react and react-native) encountered this problem already. Do you know of any good example we can take inspiration from?

@geowarin
Copy link
Contributor

Is it a good practice, idk but this pattern is used all over the place.

It is a way of saying "here are the props the lib cares about, you can pass anything else but we won't use it". As long as we maintain the "official" attribute list, we're OK.

@rafaelcorreiapoli
Copy link
Author

rafaelcorreiapoli commented Dec 31, 2017

styled-components is a library compatible with both react and react-native (and it has completely different implementations for each one). The way it's API works is by exposing functions from styled-components/native. So if you are targeting web you import from styled-components and if you are targeting react-native, you import from styled-components/native.
Maybe we could expose a different component (under react-form-react/native) for react-native usage

@sergei-zelinsky
Copy link

Hello there!

Are there any updates on this issue?

@bezenson
Copy link

bezenson commented Oct 1, 2019

One more issue with React native. onPress and handleSubmit

       <Form
          render={({ handleSubmit }): JSX.Element => (
              <TouchableOpacity onPress={handleSubmit}>
                Submit
              </TouchableOpacity>
          )}
        />

Type '(event?: SyntheticEvent<HTMLFormElement, Event> | undefined) => Promise<AnyObject | undefined> | undefined' is not assignable to type '(event: GestureResponderEvent) => void'.

@erikras
Copy link
Member

erikras commented Jan 22, 2020

Published fix in v6.3.4.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 5, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants