Skip to content

Exporting types #1684

Closed
Closed

Description

@necolas

I'm looking for input from people who use typed JavaScript with this library.

  1. If you use Flow or TypeScript, how do you type react-native-web exports?
  2. If you work across platforms, how do you deal with platform-specific differences in the APIs or components like View?
  3. Does this library need to export Flow and TypeScript types?
  4. Is there a way to generate TypeScript interfaces from Flow types so you don't need to maintain both?

Activity

  1. EvanBacon commented on Jul 24, 2020

    @EvanBacon
    Contributor

    Here is a useful dialog about TypeScript in RNW: #832

    There is no DefinitelyTyped package for react-native-web. I'm not entirely sure how one would work (efficiently). I've seen people propose copying all react-native types and then extending them, and somehow aliasing types/react-native to types/react-native-web. This of course wouldn't work with other out-of-tree solutions so I would discourage the approach.

    Personally I would opt for universal packages like "react animated" or "react text" (probably not with these names since they're all taken). These universal packages would implement iOS, Android, and web in the same place. Said approach would also fix some versioning issues and help reduce the custom bundler configuration required for universal React apps.

    I will note that RN is now moving towards a monorepo structure (creating packages like react-native/normalize-color color and react-native/polyfills) which could make modifications like this more feasible.

    At Expo, we've refactored all of our universal React packages from flow to TypeScript (as opposed to adding external type definitions files .d.ts) because we find this is easier to work with from a DX perspective.

    I personally think that having better types will help bring more visibility to important react-native-web features (e.g. href on Text). Due to the high demand in the Expo/React community for TypeScript support on web, I may look at creating some abstract packages which reexport react-native(-web) modules with proper types.

  2. paularmstrong commented on Jul 25, 2020

    @paularmstrong
    Contributor

    In Build Tracker (typescript, web-only), I pulled the react-native types from definitely-typed, then removed all of the iOS/Android specific stuff and added web-specific bits (source). Ideally I would take the few minutes to contribute this back, but I don't feel comfortable enough with my knowledge on Typescript to really be authoritative that any of it was done correctly.

    At Twitter, we have our own flow definitions for react-native-web, but had built this up before this package was exporting most of its types. Ideally, we would love to use the source here, but have found that our type definitions tend to be more strict (which we like).

    Is there a way to generate TypeScript interfaces from Flow types so you don't need to maintain both?

    There is an inverse for this using flow-typed, though it's not 100% working. I've found it work for maybe 75% of the times I've used it. The times it didn't, some light hand-editing fixed it.

  3. necolas commented on Jul 26, 2020

    @necolas
    OwnerAuthor

    @EvanBacon I don't know that much of what you suggested is actionable. I'm a bit concerned about the implication that you're going to repackage everything under different packages

    @paularmstrong I wasn't aware this package was exporting types at all 😅 Would be happy to hear about what your flow types look like and what stricter types could be upstreamed

  4. paularmstrong commented on Jul 26, 2020

    @paularmstrong
    Contributor

    Ah, they're not exporting types, correct. I believe I had done something with the module.name_mapper to re-map to the src files. It worked out okay, but not everything was fully typed at the time. And the main index.js not having // @flow at the top was problematic as well.

    I'm not as connected with that area of our code anymore, so I'm not certain what we have that's more strict. Maybe @comp615 has some thoughts on that.

    The one thing that I've noticed that's really nice about the TypeScript types from react-native that I'm using in Build Tracker is that the StyleSheet props are fully typed and throws errors if you use a style that isn't allowed (like color on a View doesn't work, but will on Text). We definitely don't have that same level of strict typing in Flow.

  5. comp615 commented on Jul 28, 2020

    @comp615
    Contributor

    I filed a ticket during the last update (pre 0.13) to look into using the RNW types by default (with Paul's mapper hack) instead of our typedef module file. As @paularmstrong mentioned, they are not currently exported in RNW, which would be a great (and I think easy?) starting point to alleviate some of the boilerplate.

    In particular, it would really help with major updates since it makes it much more trivial to track down all the things that changed and have certainty around them since they'd rely on the real source definition and not our manual list. I found it very useful last upgrade to read the release notes and update our flow file accordingly, and then go fix each usage in our code where there was a flow error.

  6. necolas commented on Jul 29, 2020

    @necolas
    OwnerAuthor

    What's the best way to export the types? Babel is currently stripping them when it compiles the files.

  7. paularmstrong commented on Jul 29, 2020

    @paularmstrong
    Contributor

    flow-copy-source or DIY copy every file over to your dist directory and add .flow as the final extension. Probably also need to ensure // @flow is at the top of every file that supports types

  8. Albert-Gao commented on Aug 10, 2020

    @Albert-Gao

    Environment:

    • expo SDK v38 (managed workflow)
    • "@types/react-native": "~0.63.6"
    • "react-native-web": "~0.13.5"
    • "typescript": "~3.9.7"
    • "react": "0.0.0-experimental-4c8c98ab9"
    • "react-dom": "0.0.0-experimental-4c8c98ab9"

    I do not use any new API from RN v0.63, so so far no problem.

    For my project, for any cross-platform components,

    two cases:

    1. the native and web share the same signature.
      • I create the components.tsx and components.web.tsx, and always use components.tsx as the source when importing.
    2. the native and web share a different signature.
      • I create a single entry like component.tsx and deal with the platform difference from there, like the above and platform check.

    And all the type info, I just use from @types/react-native

    No problems and works great.

    So, I always share the same signature across platforms. Because I do not want to propagate the platform differences to the caller, so the vast majority of the codebase can be platform agnostic.

    In short,

    I think if react-native-web can maintain an exact same type info match to @types/react-native, that will be awesome, but if there are any differences, just need to doc it, and I am fine with that, that just means, for any differences, we might need to create this one more abstraction (for example different file extension and implementation for a different platform) for every difference that we are using in the user-land code, but that is totally understandable.

    But if the API surface can be 100% percent compatible (for non-supported, won't throw an error), then it should be fine to just use the @types/react-native, otherwise, seems unnecessary and maintaining the types are a big task

    thanks for the awesome package, so beautiful.

  9. necolas commented on Sep 3, 2020

    @necolas
    OwnerAuthor

    I came here because I was shocked to find out that there's no Typescript types

    Well, RN is Flow typed and doesn't provide TS types, so I don't know what you're pretending to be so shocked about.

    I would advice leveraging on @types/react-native instead

    Those types aren't accurate for Windows or Web.

  10. aelesia commented on Sep 3, 2020

    @aelesia

    Well, RN is Flow typed and doesn't provide TS types, so I don't know what you're pretending to be so shocked about.

    Most RN libraries provide types.

    Those types aren't accurate for Windows or Web.

    Can you give examples of which parts aren't accurate?

    Offhand I can only think of ScrollView: { bounces: boolean } , which itself isn't even accurate for mobile because it's not supported on Android.

  11. JavanPoirier commented on Oct 11, 2020

    @JavanPoirier

    I'm not entirely sure how one would work (efficiently). I've seen people propose copying all react-native types and then extending them, and somehow aliasing types/react-native to types/react-native-web. This of course wouldn't work with other out-of-tree solutions so I would discourage the approach.

    @EvanBacon, At You.i TV we have a out of tree RN solution to extend to 11+ platforms and I did just that for our type definitions. I extend from the official RN types and Omit/Pick the types as needed. You mention, "This of course wouldn't work with other out-of-tree solutions so I would discourage the approach." I fail to see how this is the case. Can you please explain?

    As omitting/picking the types is a job of its own, for now I am happy to have all RN types for RN Web vs none. So I just pointed the RN types to it with: yarn add -D @types/react-native-web@npm:@types/react-native

    You can also just install @types/react-native and point the type package to react-native-web through your tsconfig.js:

    {
      "compilerOptions": {
        ...
        "baseUrl": "./", // Required with the use of paths. May differ depending on project structure.
        "paths": {
          "@types/react-native-web": ["./node_modules/@types/react-native"]
        },
        ...
      }
    }
    
  12. JavanPoirier commented on Oct 11, 2020

    @JavanPoirier

    @necolas, I could look into writing the initial TypeScript definition file for this library. I see that the Flow types are include in the repo. I am not experienced with Flow, however would you like to the types to be included here (like Flow) or in Definitely Typed?

  13. nandorojo commented on Oct 19, 2020

    @nandorojo
    Contributor

    Could TypeScript declaration merging solve this?

    That's what I'm using in my current project.

    Example

    // react-native-web/overrides.ts
    
    declare module 'react-native' {
      interface PressableStateCallbackType {
        hovered: boolean
      }
    }

    That would extend the existing PressableStateCallbackType from @types/react-native (with one caveat, more on that below.)

    Screen Shot 2020-10-19 at 12 32 15 PM

    Caveat

    There is one problem with this approach at the moment. @types/react-native exports certain types as type rather than interface. This prevents declaration merging.

    For instance, this is the current type for Pressable's callback:

    // @types/react-native/index.d.ts line 473
    export type PressableStateCallbackType = Readonly<{
        pressed: boolean;
    }>;

    Changing it to this makes it extensible:

    export interface PressableStateCallbackType {
        pressed: boolean;
    }

    Thus, declaration merging could (in a few cases) require a PR to @types/react-native turning type into interface.

  14. fardad-dev commented on Oct 22, 2020

    @fardad-dev
    Contributor

    I think ReactNative could have web type too. ReactNative supported specific type for android or iOS that could support specific type for web too.

  15. 18 remaining items

  16. added this to the 0.16 milestone on Apr 9, 2021
  17. added a commit that references this issue on Apr 9, 2021
    30a4791
  18. added a commit that references this issue on Apr 20, 2021
    c4e2d7a
  19. fardad-dev commented on Apr 21, 2021

    @fardad-dev
    Contributor

    @necolas is it solved typescript issue?

  20. nandorojo commented on Apr 21, 2021

    @nandorojo
    Contributor

    The current commit doesn't fix it for TypeScript users.

    I mentioned the TS solution in comment above.

    react-native-tvos uses the strategy I mentioned. You can see their types declaration file here.

    Maybe there's a way to turn the generated flow files into TS types.

  21. kopax-polyconseil commented on Jan 10, 2022

    @kopax-polyconseil

    I am experience errors with accessibilityRole typing on the Web : #2189 (comment)

    Is there a support for TypeScript here we can use or a workaround ?

  22. orlando commented on Jan 27, 2022

    @orlando

    I am experience errors with accessibilityRole typing on the Web : #2189 (comment)

    Is there a support for TypeScript here we can use or a workaround ?

    I answered this here #2194 (comment).

    Tl;dr add this to your types file.

    declare module 'react-native' {
      interface TextProps {
        // https://github.com/necolas/react-native-web/blob/master/packages/react-native-web/src/modules/AccessibilityUtil/propsToAccessibilityComponent.js#L12
        accessibilityLevel?: number;
        accessibilityRole?:
          | 'heading'
          | 'label'
          | 'list'
          | 'listitem'
          | 'main'
          | 'region'
          | 'strong';
      }
    }
  23. yawnr commented on Mar 30, 2022

    @yawnr

    @nandorojo pardon my ignorance, but doesn't declaring the module like that wipe out all of the existing type definitions from @types/react-native? If I do something like

    // types/rn-web.d.ts
    import * as RNTypes from 'react-native';
    
    declare module 'react-native' {
      export type ImageRequireSource = string;
    }
    

    then all of my existing RN imports error with a no exported member error. Is there some config you're using to be able to extend the module declaration?

  24. nandorojo commented on Mar 31, 2022

    @nandorojo
    Contributor

    Add this:

    import 'react-native'

    See: https://github.com/nandorojo/solito/blob/master/example-monorepos/blank/packages/app/rnw-overrides.tsx

    I used .tsx there, I believe that worked for me.

  25. added a commit that references this issue on Sep 13, 2022
    4535ea8
  26. juanpprieto commented on Jun 25, 2023

    @juanpprieto

    This did it for me

    npm i --save-dev @types/react-native-web@npm:@types/react-native
  27. erickreutz commented on Jan 19, 2024

    @erickreutz

    What is the current state of this with typescript?

  28. blazejkustra commented on Aug 7, 2024

    @blazejkustra
    Contributor

    There is no DefinitelyTyped package for react-native-web. I'm not entirely sure how one would work (efficiently).

    Until now. Happy to announce that types for react-native-web are now available on DefinitelyTyped! 🎉

    The package comes with a react-native-web global declaration, so you can use it in your project type-safe.

    import { AppRegistry } from 'react-native-web';

    And it also extends the react-native types, so you can use react-native components in your react-native-web project.

    import { View } from 'react-native';
    
    <View style={{ position: 'fixed' }} />

    Installation

    npm install --save-dev @types/react-native-web

    To extend the react-native types, you have to supply react-native-web as a member of the types compiler option in tsconfig.json.

    {
      "compilerOptions": {
        "types": ["react-native-web"]
      }
    }
  29. sahajarora1286 commented on Oct 14, 2025

    @sahajarora1286

    This is still a problem for me in my react-native-web monorepo project.

    I already have "react-native-web" added to compilerOpitons in tsconfig but it doesn't get rid of the TS errors on react-native-web props on react-native components.
    However, if I add the following tsconfig alias mapping:
    "react-native": ["node_modules/react-native"] ,
    then the TS errors are gone; BUT this is a problem because webpack (the bundler) now tries to load flow files from react-native package and fails to do so (obviously). This alias mapping is forcing webpack to use 'react-native' instead of 'react-native-web' ; which is already added as an alias in webpack config :

    resolve: {
          // This will only alias the exact import "react-native"
          alias: {
            "react-native$": "react-native-web",
            "react-native": "react-native-web",
            "tailwind-global.css": path.join(__dirname, "../tailwind-global.css"),
          },
    }
    

    I have been struggling with this for many days now and any help would be highly appreciated!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions