-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Exporting types #1684
Comments
Here is a useful dialog about TypeScript in RNW: #832 There is no DefinitelyTyped package for 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 I personally think that having better types will help bring more visibility to important react-native-web features (e.g. |
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. |
@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 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 |
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. |
What's the best way to export the types? Babel is currently stripping them when it compiles the files. |
|
Environment:
For my project, for any cross-platform components,two cases:
And all the type info, I just use from 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 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. |
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. |
Most RN libraries provide types.
Can you give examples of which parts aren't accurate? Offhand I can only think of |
@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: You can also just install
|
@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? |
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 CaveatThere is one problem with this approach at the moment. For instance, this is the current type for // @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 |
I think ReactNative could have web type too. ReactNative supported specific type for android or iOS that could support specific type for web too. |
I don't think React Native wants that as the react-native-macos, react-native-web, react-native-windows are not 'official'. Maybe it would be better to create a different type system which has special properties for all these platforms available, but that would require a lot of work. Maybe we will have to wait for React Native to add iOS Pointer support as this would probably create the hovered in React Native itself for iOS: |
I do not agree with you because React Native added web to official documentation but not added react-native-macos or react-native-windows |
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? |
In my app, I use I then use declaration merging in my own files. Until there is a better solution, I'll be sticking with that. I can put together a gist if it would be useful. Re: the questions:
I'm not a big fan of creating my own |
What about when you need to overwrite an existing field? For example if I need to add "fixed" in position type:
It complains about |
I've tried to avoid fixed position and things that RN doesn't support, but when you can't, you might need to use patch-package to edit the react native types package. |
@baptisteArno Should work if you change |
Some good news: You can now do this in your app: // react-native-web/overrides.ts
declare module 'react-native' {
interface PressableStateCallbackType {
hovered?: boolean
focused?: boolean
}
} And you'll get type support for I think it would make sense to have one TS file in |
Sorry to keep adding messages here, but I think the solution is to add a declare module 'react-native' {
interface PressableStateCallbackType {
hovered?: boolean
focused?: boolean
}
interface ViewStyle {
transitionProperty?: string
transitionDuration?: string
}
// ...etc
} I only added a few types there, but it would be easy to add the rest of the RNW-specific props. This would solve the problem for TypeScript users. |
You should install react-native types
And redirect them to react-native-web types in tsconfig.json
|
I played around most of today with Flow and trying to get the types to just pop up using flow-copy-source. I was able to get flow updated to the latest version, including re-copying some of the vendored code from RN, and doing some code mods. See that all here: https://github.com/comp615/react-native-web/tree/flow. However, when I tried to use this in a sample expo projects. I still got a fair number of flow errors from:
The code to update flow from that branch is 99% good, with the package change to copy sources being the bit I'd pull out. I'd like to try it again with a create-react-app or Twitter to see if a different project setup works more easily. Expo seems to just work with react-native because RN is directly linked from source and not compiled through babel. As a side musing...flow internally via types-first seems to keep some externally visible signature for each file. It would be awesome if it would just dump those as .flow.js files so we didn't have to copy source as lib authors. |
@necolas is it solved typescript issue? |
The current commit doesn't fix it for TypeScript users. I mentioned the TS solution in comment above.
Maybe there's a way to turn the generated flow files into TS types. |
I am experience errors with 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';
}
} |
@nandorojo pardon my ignorance, but doesn't declaring the module like that wipe out all of the existing type definitions from
then all of my existing RN imports error with a |
Add this: import 'react-native' I used |
This did it for me npm i --save-dev @types/react-native-web@npm:@types/react-native |
What is the current state of this with typescript? |
Until now. Happy to announce that types for The package comes with a import { AppRegistry } from 'react-native-web'; And it also extends the import { View } from 'react-native';
<View style={{ position: 'fixed' }} /> Installationnpm install --save-dev @types/react-native-web To extend the {
"compilerOptions": {
"types": ["react-native-web"]
}
} |
I'm looking for input from people who use typed JavaScript with this library.
react-native-web
exports?View
?The text was updated successfully, but these errors were encountered: