You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm looking for input from people who use typed JavaScript with this library.
If you use Flow or TypeScript, how do you type react-native-web exports?
If you work across platforms, how do you deal with platform-specific differences in the APIs or components like View?
Does this library need to export Flow and TypeScript types?
Is there a way to generate TypeScript interfaces from Flow types so you don't need to maintain both?
Reacted by Tom McIntosh, Fernando Rojo, Theresa Rapior, Christophe Menager, Mateja Petrovic, Orlando Del Aguila, Barry Michael Doyle, Raphael and Jack Coy
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.
Reacted by Brent Vatne, Daniel Steigerwald, tje512, aelesia, Petr Bela, Jules Sam. Randolph, KingAmo, Jack Tomaszewski, Tom McIntosh, Richard Willis and 6 more
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.
@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
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.
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.
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
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:
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.
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
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"]
},
...
}
}
@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?
That would extend the existing PressableStateCallbackType from @types/react-native (with one caveat, more on that below.)
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 473exporttypePressableStateCallbackType=Readonly<{pressed: boolean;}>;
@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?
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!
Activity
EvanBacon commentedon Jul 24, 2020
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 allreact-nativetypes and then extending them, and somehow aliasingtypes/react-nativetotypes/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.
hrefonText). 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.paularmstrong commentedon Jul 25, 2020
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).
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.
necolas commentedon Jul 26, 2020
@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
paularmstrong commentedon Jul 26, 2020
Ah, they're not exporting types, correct. I believe I had done something with the
module.name_mapperto re-map to thesrcfiles. It worked out okay, but not everything was fully typed at the time. And the mainindex.jsnot having// @flowat 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
coloron aViewdoesn't work, but will onText). We definitely don't have that same level of strict typing in Flow.comp615 commentedon Jul 28, 2020
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.
necolas commentedon Jul 29, 2020
What's the best way to export the types? Babel is currently stripping them when it compiles the files.
paularmstrong commentedon Jul 29, 2020
flow-copy-sourceor DIY copy every file over to yourdistdirectory and add.flowas the final extension. Probably also need to ensure// @flowis at the top of every file that supports typesAlbert-Gao commentedon Aug 10, 2020
Environment:
"@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"For my project, for any cross-platform components,
two cases:
components.tsxandcomponents.web.tsx, and always usecomponents.tsxas the source when importing.component.tsxand deal with the platform difference from there, like the above and platform check.And all the type info, I just use from
@types/react-nativeNo 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.
necolas commentedon Sep 3, 2020
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.
Those types aren't accurate for Windows or Web.
aelesia commentedon Sep 3, 2020
Most RN libraries provide types.
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.JavanPoirier commentedon Oct 11, 2020
@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-nativeYou can also just install
@types/react-nativeand point the type package toreact-native-webthrough yourtsconfig.js:JavanPoirier commentedon Oct 11, 2020
@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?
nandorojo commentedon Oct 19, 2020
Could TypeScript declaration merging solve this?
That's what I'm using in my current project.
Example
That would extend the existing
PressableStateCallbackTypefrom@types/react-native(with one caveat, more on that below.)Caveat
There is one problem with this approach at the moment.
@types/react-nativeexports certain types astyperather thaninterface. This prevents declaration merging.For instance, this is the current type for
Pressable's callback:Changing it to this makes it extensible:
Thus, declaration merging could (in a few cases) require a PR to
@types/react-nativeturningtypeintointerface.fardad-dev commentedon Oct 22, 2020
I think ReactNative could have web type too. ReactNative supported specific type for android or iOS that could support specific type for web too.
18 remaining items
[change] Add generated Flow files to package dist
[change] Add generated Flow files to package dist
fardad-dev commentedon Apr 21, 2021
@necolas is it solved typescript issue?
nandorojo commentedon Apr 21, 2021
The current commit doesn't fix it for TypeScript users.
I mentioned the TS solution in comment above.
react-native-tvosuses 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.
kopax-polyconseil commentedon Jan 10, 2022
I am experience errors with
accessibilityRoletyping on the Web : #2189 (comment)Is there a support for TypeScript here we can use or a workaround ?
orlando commentedon Jan 27, 2022
I answered this here #2194 (comment).
Tl;dr add this to your types file.
yawnr commentedon Mar 30, 2022
@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 likethen all of my existing RN imports error with a
no exported membererror. Is there some config you're using to be able to extend the module declaration?nandorojo commentedon Mar 31, 2022
Add this:
See: https://github.com/nandorojo/solito/blob/master/example-monorepos/blank/packages/app/rnw-overrides.tsx
I used
.tsxthere, I believe that worked for me.[change] Add generated Flow files to package dist
juanpprieto commentedon Jun 25, 2023
This did it for me
erickreutz commentedon Jan 19, 2024
What is the current state of this with typescript?
blazejkustra commentedon Aug 7, 2024
Until now. Happy to announce that types for
react-native-webare now available on DefinitelyTyped! 🎉The package comes with a
react-native-webglobal declaration, so you can use it in your project type-safe.And it also extends the
react-nativetypes, so you can usereact-nativecomponents in yourreact-native-webproject.Installation
To extend the
react-nativetypes, you have to supplyreact-native-webas a member of the types compiler option in tsconfig.json.{ "compilerOptions": { "types": ["react-native-web"] } }sahajarora1286 commentedon Oct 14, 2025
This is still a problem for me in my react-native-web monorepo project.
I already have
"react-native-web"added tocompilerOpitonsintsconfigbut 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-nativepackage 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 inwebpackconfig :I have been struggling with this for many days now and any help would be highly appreciated!