How can I use the type from TypeScript for flowtype in React Native - typescript-typings

I use VSCode IDE to develop React Native apps. I found that the IDE provides type info from TypeScript, but I can't use it directly with flowtype.
For example
render = () => (
<WebView
onNavigationStateChange={this.onNavigationStateChange}
/>
)
onNavigationStateChange = (navState: NavState) => {
//...
};
The type NavState could be found in the index.d.ts provided by VSCode, but it can't be used in the flow.js.
Can I use types from TypeScript files in my React Native project?
If not, how can I know the correct types in React Native?

It's not possible. You could transform the .d.ts files into .flow.js files. Although TypeScript looks pretty much like Flow, it doesn't support the same features. So I don't think that Flow will ever support .d.ts files completely. Maybe they'll support parts of them in the future. Here is the Github issue to this topic.

Related

Use Typescript in some part of the app only

I have a node js application.
All the codes are now in javascript.
But in some part of my app I want to use Typescript Interface or enum options.
How can I use Typescript just for those part of the app?
I don't want to refactor all the project to using Typescript.
You can try Flow https://flow.org
Flow in opposition to TypeScript isn't a superset or language. It's a static type checker for JavaScript. Because it's not a language, it can be smoothly integrated with JavaScript with a single comment annotation.
You can add a single .ts file, and use plain javascript for the rest of them.
https://www.typescriptlang.org/
TypeScript is a typed superset of JavaScript that compiles to plain
JavaScript.
[...] Use existing JavaScript code, incorporate popular JavaScript
libraries, and call TypeScript code from JavaScript.
TypeScript compiles to clean, simple JavaScript code which runs on any
browser, in Node.js, or in any JavaScript engine that supports
ECMAScript 3 (or newer).

TypeScript and Node

Are there any limitations using TypeScript in a Node project compared to using Javascript at the server side for a web application?
Are all existing Node modules out there totally reusable without modification?
(PS: I am ok to use Visual Studio as the IDE.)
Are there any limitations using TypeScript in a Node project compared to using Javascript
No. You just have the overhead of setting up TypeScript, for the sake of its advantages : https://basarat.gitbooks.io/typescript/content/docs/why-typescript.html
Are all existing Node modules out there totally reusable without modification?
Yes. Just set allowJs:true in your tsconfig and you don't even need a declaration file.

Sharing code between node js project and a web site project

So I started coding a chess engine in typescript as a side project. I initially intended it to be a CLI like most other chess engines that interact through stdin and stdout. This so I could plug it to a GUI like Arena and test it against other engines. For that I decided to do it as a node project.
I set it across several files with 1 being the UCI (universal chess interface) implementation and the remaining ones chess logic and AI logic. After banging my head a few times trying to understand modules in typescript I finally got it to work.
Now I decided it would be interesting to write a simple GUI myself as a webpage with html+typescript+jquery. Now, I would like to use all the logic modules I wrote but I'm finding it impossible. From what I understand you can't use CommonJS in browsers so the only way I could get it to work was using instead internal modules for which I need to modify the .ts files to wrap the code in module X{ } blocks and recompile them just for the GUI every time I change something. This situation seems far from ideal and I was wondering if there is a way around it...
Use http://browserify.org/ to add CommonJS-like support on the front-end.
But also read this (slightly outdated) question and answer: How should I go about writing a node.js web application with both server and client side code?
Use external modules for both the server and the browser.
When compiling for the browser, use the switch to specify the AMD module pattern:
tsc --module amd app.ts
And use RequireJS to load modules for you.
<script src="require.js" data-main="app.js"></script>
You will need to compile for the different targets, but the source code in TypeScript can be identical.
I created a demo project to share code between the client and the server : https://github.com/basarat/demo-fullstack/blob/master/src/Gruntfile.js
It compiles the common files for both amd/commonjs and server files only as commonjs with the client only files as only amd. It uses grunt-ts to manage this : https://github.com/grunt-ts/grunt-ts

Does TypeScript require node.js to run?

Installing TypeScript requires node.js. And I assume that TypeScript uses Node.js to compile the .ts to a .js file.
My question is, does that created .js file require node.js? The ones I've seen so far appear not to. I don't want to load node.js into my html pages if it's not used.
thanks - dave
No, TypeScript just emits regular JavaScript.
If you use the "external modules" feature of the language (import x = require('foo');) you'll need to compile for either CommonJS (node) or AMD (require.js) and have those available, but that's opt-in.

Open existing Node.js Project in a TypeScript project

I currently have an existing project in Node.js and I would like to migrate it to a TypeScript project.
How can I do this?
Thanks for the help.
The easiest way to do this is to look at the Node sample provided with the TypeScript. It already includes declarations for the built-in NodeJS modules.
After importing the node.js you can start by converting your modules one by one. Since the Typescript will convert into JS, you can convert one file at a time and test it right away!
With ambients it should be easy to do! Good luck with your conversion.

Resources