简体   繁体   English

是否可以在 nativescript 项目中一起使用 typescript 和 javscript

[英]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.我想知道我是否可以在一个项目中使用 javascript 和 typescript 以及如何使用。 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我有一个现有的 nativescript 核心 js 项目,我想使用一个插件,该演示是用 TS 编写的,它是一个webrtc 插件,所以我对它的理解不够到开始弄乱代码

i have tried running tns install typescript before but it messed up my project.我之前尝试过运行tns install typescript但它搞砸了我的项目。

Basic considerations:基本考虑:

  • When you use TypeScript, you have to know it is not a runtime language, is a compiled language.当您使用 TypeScript 时,您必须知道它不是一种运行时语言,而是一种编译语言。
  • 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.当您编译 TypeScript 文件、项目或模块时,TypeScript 编译器 (tsc) 将根据项目(在 TypeScript 中)config.ts 文件中定义的 ECMAScript 规范生成 JavaScript 文件(通常是 CommonJS 模块)。
  • 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.如果你需要在 JavaScript 中使用插件,但它是用 TypeScript 编写的,你必须先编译它,然后在你的 JavaScript 项目中导入模块。

Advanced considerations:高级注意事项:

  • The ECMAScript specification version defined in the config.ts file, need to be compatible with your project JavaScript version. config.ts 文件中定义的 ECMAScript 规范版本,需要与您的项目 JavaScript 版本兼容。
  • 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.即使您使用符合 ES6 规范的 JavaScript,也无法使用相同的执行上下文将 TypeScript 集成到 JavaScript 项目中。 Remember, JavaScript is an interpreted language, not compiled.请记住,JavaScript 是一种解释型语言,而不是编译型语言。

Conclusions:结论:

Consider to migrate your code to TypeScript, compile the plugin and add it manually or search an alternative for that plugin in JavaScript.考虑将您的代码迁移到 TypeScript,编译插件并手动添加它或在 JavaScript 中搜索该插件的替代品。


PD: Check this related issue in the nativescript-webrtc GitHub repository PD:在nativescript-webrtc GitHub 存储库中检查相关问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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