Can I use TS classes/interfaces in a simple JS mean application? - javascript

Let's say that I have a build up MEAN application written in JS, and I want to keep it but add some classes/interfaces using TS (I want to do this because I want interfaces) and use it in my controllers to communicate with my backend. Is it possible? If yes, how do i do it?
Thanks in advance!

Since TypeScript is a superset of javascript all JS code will work in typescript BUT you need to change the file extensions from .js to .ts
Furthermore, all typescript code must be compiled back to javascript before run. which means a typescript compiler has to be installed and configured.
That was my answer to your question.
As a piece of advice since you are building with MEAN stack then you are used to Angular way of structuring code.
If that is the case then you can build your server the same way with TypeScript using a framework called NestJS which is very very similar to Angular:
https://nestjs.com

Related

is it possible to use typescript and javscript together in a nativescript project

i would like to know if i can use javascript and typescript in one project and how.
i have an existing nativescript core js project, i want to use a plugin, the demo is written in TS, and its a webrtc plugin so i don't understand it enough to start messing with the code
i have tried running tns install typescript before but it messed up my project.
Basic considerations:
When you use TypeScript, you have to know it is not a runtime language, is a compiled language.
When you compile a TypeScript file, project or module, the TypeScript compiler (tsc) will generate the JavaScript files (usually CommonJS modules) according to the ECMAScript specification defined in the project (in TypeScript) config.ts file.
If you need to use a plugin in JavaScript but it is programmed in TypeScript, you have to compile it first, and then import the module/s in you JavaScript project.
Advanced considerations:
The ECMAScript specification version defined in the config.ts file, need to be compatible with your project JavaScript version.
There is no way to integrate TypeScript in a JavaScript project using the same execution context, even if you use JavaScript with the ES6 specification. Remember, JavaScript is an interpreted language, not compiled.
Conclusions:
Consider to migrate your code to TypeScript, compile the plugin and add it manually or search an alternative for that plugin in JavaScript.
PD: Check this related issue in the nativescript-webrtc GitHub repository

How to translate a tsx file to a js file for React

I am working on a React Project and have a bunch of tsx files that I want to convert as use as javascript for my project. How should I do that?
I am working on a React Project and have a bunch of tsx files that I want to convert as use as javascript for my project. How should I do that
You can use the typescript compiler e.g.
npx -p typescript tsc somefile.tsx
Ciao, while the web is full of guide on how to translate React Javascript into Typescript, I don't found any reverse guide (so strange...).
Anyway, I had same problem some time ago and I found useful get one of those guide JS => TS for React and read backwards. For example, I used this guide. When he said "JavaScript looks like that so you can translate this into Typescript in this way..." I did the contrary (and it worked!). Of course you can skip all the dependencies part.
Try and let us know.

Can I develop Node.js (Express) app in both JavaScript and TypeScript together?

I have Node.js project (Express REST API) written in JavaScript.
We decided to rewrite whole project to TypeScript. Is possible to do it slowly, part by part, but still be able to start server?
I want rewrite just some parts so project will be partly JS partly TS. Is it possible?
Does ts-node-dev compile ts and ignore js?
Yes this should be possible based on a main principle of TypeScript: TS is a superset of JS and all JS is also legal TS.
If you want to be particularly granular in how you are migrating your project, you should configure the TS compiler to only pay attention to files in which you are implementing the change from JS to TS.
It's possible to do that, this kind of operation is called Migrating javascript to typescript, but if you tend to keep it half JS and half TS then it's logically wrong, because Typescript is transpiled : translated and compiled into java script, in a machinery coded manner. so having java script code with ts is basically not a good practice.
You can allow js by the using the compiler flag --allowJs.
thus I think this post has more illustration.

Using a browser-ified module in an app that then needs to be browser-ified

I have written a self contained angular js module using browserify in order to make use of the commonJS/Node style syntax.
The module works fantastic when tested by itself, so I then use gulp to minify and host that on GitHub.
I've then imported that into another app that is also using browserify. When I run browserify it seems to try and rebrowserify the module and causes no end of problems.
I believe this is because the module requires angular and jquery and qtip2. So it's obviously trying to re parse these.
Is there a standard to not parse modules, or is there a way to exclude the browserifying of the modules? Or is it best to not include things like angular and jquery within your modules? I was trying to make them perfectly stand alone, maybe that's unwise?
Many thanks!
I would suggest providing both options, if it is important for you to have a standalone version that includes angular. This will provide people using your code with a total of three ways of using your code: Using the standalone version, the version that only includes the module, and cloning the repository directly and including the source files as part their build process.
I generally use the third option, but people who don't have build processes will likely prefer the first or second.

Jade lang in a regular web app without node

Is it possible to use Jade in a regular web app without running on Node js? The question may sound crazy as Jade engine is written in node but wanted to find out if it can can be used oustide of Node.
So, it all depends what you really want.
You can compile a function to jade directly, through the CLI :
jade -w index.jade (-w is "watch for change", auto-recompiles)
or, if you use another language, you may be interested in other implementations :
PHP (I think you may find forks maintained)
Scala
Ruby
Python
Java
Yes, you just need to somehow get the jade compiler to run at the right time. You could accomplish that manually though a watcher script or with a build environment like nodefront.

Categories