react-native icon indicating copy to clipboard operation
react-native copied to clipboard

React Native components with Typescript

Open Thegaram opened this issue 11 years ago • 64 comments

Hi! Is it possible to implement React Native components with TypeScript?

What I want to be able to do is this:

import React = require('react-native');
class MyClass<P, S> extends React.Component<P, S> { ... }

I figured I might need to create a type definition file for React Native (like the one for React.js). Is there any reason why nobody has created this file yet?

Thegaram avatar Aug 31 '15 12:08 Thegaram

Wold love to see how we can use react native with typescript as well. +1

thanzen avatar Aug 31 '15 15:08 thanzen

We're focused on flow instead of typescript because it is a much more powerful tool for inferring program structure and verifying correctness, so I don't think we have any plans to support typescript at this time.

sahrens avatar Aug 31 '15 17:08 sahrens

More info on flow: http://flowtype.org/

sahrens avatar Aug 31 '15 17:08 sahrens

@sahrens Do you have any examples or specific cases where flow's type inference comes out ahead of Typescript? The only place Typescript is mentioned on http://flowtype.org/ is about future plans for adding support for converting existing TypeScript declaration files. I am having a hard time identifying any advantage of using flow instead of Typescript. Thx!

bparadie avatar Sep 01 '15 05:09 bparadie

@bparadie a big advantage is that flow is more like a linter than a compiler. if you ask in the flow IRC chat i'm sure you'll get some good responses.

ide avatar Sep 01 '15 06:09 ide

@sahrens Thanks for the clarification! Just to make it clear: It is theoretically possible to use React Native with TypeScript if I define the definition file, right? (Or would it somehow interfere with flow?)

Thegaram avatar Sep 01 '15 08:09 Thegaram

typescript and flow should be independent, although the ReactNative core is annotated for flow so those annotations probably won't work with typscript very well (although they are similar). You can definitely annotate your own files however you want, it's just that the type checking might not interoperate with core files very well.

Also note that flow is designed to be incremental, whereas it's hard to get a lot of value out of typescript without doing a full conversion of your entire project (or so I've heard).

sahrens avatar Sep 03 '15 20:09 sahrens

@gabelevi: anything to add?

sahrens avatar Sep 03 '15 20:09 sahrens

@Thegaram I have used react-native with Typescript by creating my own react-native.d.ts derived from react.d.ts. By "derived" I mean this:

declare module "react-native" {
  import * as React from 'react';
  import {ComponentClass, ReactType, ReactNode, ReactElement} from 'react';  
  export * from 'react';
  ...
}

There is no react-native section under DefinitelyTyped yet and perhaps I should donate my react-native.d.ts. The only problem is that it is very incomplete as I have been adding definitions only for the APIs and classes I needed.

UPDATE: Here is my react-native.d.ts: https://gist.github.com/bparadie/93cc1078d1db8fcd3c4e

bparadie avatar Sep 05 '15 16:09 bparadie

@ide Why is it a big advantage to have a linter instead of a real compiler? Especially after observing that the compiler's performance seems to be better than the linter's. I am not trying to pick up a bar fight here. But last time I checked flow requires you to annotate your code at module boundaries.

What I don't understand is: If you have to add type information at one point anyways, why not just using a "real" language. I haven't heard of any linter that requires type annotations. Don't get me wrong, I like react-native and I don't have a big problem with the architectural choices you guys have made.

But looking at your dev tool chain I am seeing a flow linter that checks the code before babel polyfills your ES6 JS to "regular" JS. The babel step is in my opinion a cross-compilation step without type checking. What doesn't make sense to me is using two separate tools (flow and babel) that should be handled by one cross-compiler. That's why I recommend using Typescript and disabling flow for react-native development.

bparadie avatar Sep 05 '15 16:09 bparadie

@Thegaram Wanted to add that you should file a "definition request" issue at DefinitelyTyped. But you've already done that: https://github.com/borisyankov/DefinitelyTyped/issues/5634

bparadie avatar Sep 05 '15 17:09 bparadie

@bparadie I was working on a similar solution, but yours is much more detailed, thanks for sharing!

Thegaram avatar Sep 05 '15 18:09 Thegaram

@Thegaram You are welcome! I am sure you already know that Typescript 1.6 beta has been released with support for react/JSX. I came up with my own solution for translating JSX that I can now hopefully retire. TSC 1.6 will make using react and react-native so much easier!

One aspect that this thread about flow/babel vs typescript has not covered yet is the IDE. I personally like Visual Studio Code better than Facebook's Nuclide. But I have to admit that I haven't played with Nuclide much. You could probably even mix and match (using Nuclide for TS and Code for flow/babel). Just wanted to throw this into the mix of things to consider when picking flow/babel or typescript...

bparadie avatar Sep 06 '15 18:09 bparadie

Flow and babel were developed independently around the same time so couldn't depend on each other. There are discussions about how to converge them. It would be great to have a single system there, which would also open up a ton of sweet cross-boundary/whole program optimizations we could make, like automatic dead code elimination and inlining. On Sep 6, 2015 11:18 AM, "Bernd Paradies" [email protected] wrote:

@Thegaram https://github.com/Thegaram You are welcome! I am sure you already know that Typescript 1.6 beta has been released with support for react/JSX http://blogs.msdn.com/b/typescript/archive/2015/09/02/announcing-typescript-1-6-beta-react-jsx-better-error-checking-and-more.aspx. I came up with my own solution for translating JSX that I can now hopefully retire. TSC 1.6 will make using react and react-native so much easier!

One aspect that this thread about flow/babel vs typescript has not covered yet is the IDE. I personally like Visual Studio Code https://code.visualstudio.com/ better than Facebook's Nuclide http://nuclide.io/. But I have to admit that I haven't played with Nuclide much. You could probably even mix and match (using Nuclide for TS and Code for flow/babel). Just wanted to throw this into the mix of things to consider when picking flow/babel or typescript...

— Reply to this email directly or view it on GitHub https://github.com/facebook/react-native/issues/2502#issuecomment-138105513 .

sahrens avatar Sep 07 '15 02:09 sahrens

(Note the creators of Babel and Flow now work on the same Facebook JS Infra team now, so it's even easier for them to collaborate.) On Sep 6, 2015 7:38 PM, "Spencer Ahrens" [email protected] wrote:

Flow and babel were developed independently around the same time so couldn't depend on each other. There are discussions about how to converge them. It would be great to have a single system there, which would also open up a ton of sweet cross-boundary/whole program optimizations we could make, like automatic dead code elimination and inlining. On Sep 6, 2015 11:18 AM, "Bernd Paradies" [email protected] wrote:

@Thegaram https://github.com/Thegaram You are welcome! I am sure you already know that Typescript 1.6 beta has been released with support for react/JSX http://blogs.msdn.com/b/typescript/archive/2015/09/02/announcing-typescript-1-6-beta-react-jsx-better-error-checking-and-more.aspx. I came up with my own solution for translating JSX that I can now hopefully retire. TSC 1.6 will make using react and react-native so much easier!

One aspect that this thread about flow/babel vs typescript has not covered yet is the IDE. I personally like Visual Studio Code https://code.visualstudio.com/ better than Facebook's Nuclide http://nuclide.io/. But I have to admit that I haven't played with Nuclide much. You could probably even mix and match (using Nuclide for TS and Code for flow/babel). Just wanted to throw this into the mix of things to consider when picking flow/babel or typescript...

— Reply to this email directly or view it on GitHub https://github.com/facebook/react-native/issues/2502#issuecomment-138105513 .

sahrens avatar Sep 07 '15 02:09 sahrens

@sahrens Thanks for clarifying. One combined flow/babel tool would be cool!

bparadie avatar Sep 08 '15 19:09 bparadie

Also note that flow is designed to be incremental, whereas it's hard to get a lot of value out of typescript without doing a full conversion of your entire project (or so I've heard).

My experience has been pretty smooth with small projects but there does appear to be agreement upstream that the experience could be better and Flow's approach is influencing that - https://github.com/Microsoft/TypeScript/issues/4094

One really valuable thing that TypeScript has is a large library of typings for existing third-party libraries, DefinitelyTyped. AIUI there isn't an equivalent for Flow at the moment. It would be really helpful if Flow / TS could converge in the places where the syntax currently differs so that Flow could leverage these, and vice versa.

robertknight avatar Sep 19 '15 12:09 robertknight

By the way, TypeScript is far better than Flow in my case, since the other developer I'm working with uses Windows exclusively, and TypeScript has better ES6 support. I would love TypeScript support.

dead-claudia avatar Oct 20 '15 17:10 dead-claudia

+1 for official typescript support as well.

davidLeonardi avatar Oct 27 '15 00:10 davidLeonardi

+1 for official typescript support! Would love to see this support.

rupreetg avatar Nov 03 '15 08:11 rupreetg