简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM