在Node.js中混合使用JavaScript和TypeScript [英] Mixing JavaScript and TypeScript in Node.js

查看:744
本文介绍了在Node.js中混合使用JavaScript和TypeScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然在ES6中有部分Node.js,但是可以在同一个项目中混合一些Typescript模块吗?

While having parts of a Node.js in plain ES6, is it possible to mix in some Typescript modules within the same project?

例如。在TypeScript中定义了一些通过 require 导入普通ES6文件的类型?

E.g. having some Types defined in TypeScript that are imported via require into plain ES6 files?

推荐答案

是的,这是可能的。

结合以下编译器标志


  1. - allowJs

明确支持混合的JavaScript和TypeScript源

Explicitly supports mixed JavaScript and TypeScript sources

- outDir

由于所有文件都将被转换,因此必须将生成的JavaScript输出到另一个目录,否则输入
.js 文件将被覆盖 1

Since all files will be transpiled, it is necessary to output the resulting JavaScript into a different directory otherwise the input .js files would be overwritten1.

- checkJs

这是完全可选的。如果指定,编译器将检查JavaScript文件,报告错误,就像在TypeScript文件中一样,否则会容忍不一致。

This is completely optional. If specified, the compiler will typecheck JavaScript files, reporting errors just as in TypeScript files, where as it would otherwise tolerate inconsistencies.

至于在JavaScript文件中使用TypeScript文件中声明的类型,确实可以这样做。

As to using types declared in a TypeScript file in a JavaScript file, this can indeed be done.

TypeScript实际上支持Visual等工具中的所有JavaScript智能感知工作室代码。

TypeScript actually powers all of the JavaScript intellisense in tools like Visual Studio Code.

类型可以放在JSDoc 2 注释中。这些注释可以引用从TypeScript导入的类型( .ts / .tsx / .d。 ts )文件。像Visual Studio Code这样的IDE将在这些注释中提供语法高亮和自动完成。

Types may be placed in JSDoc2 comments. These comments can reference types imported from TypeScript (.ts/.tsx/.d.ts) files. IDEs like Visual Studio Code will provide syntax-highlighting and auto completion within these comments.

但是有一点需要注意。由于JavaScript中的类型没有明确的语法,因此无法单独导入,但必须附加到导入的。这通过TypeScript的声明合并最方便地实现,如下所示。

There's a caveat however. Because there's no manifest syntax for types in JavaScript, they cannot be imported individually but must be attached to a value which is imported. This is most conveniently achieved via TypeScript's declaration merging as shown below.

示例:

a。 ts

export default createThing;

function createThing(...args): createThing.Thing  {...}

namespace createThing {
  export interface Thing {...}
}

b.js

import createThing from './a';

/**
 * @param {createThing.Thing} thing
 */
export function takesThing(thing) {}

注意:

1 - 如果另外指定 - noEmit 标志,则不需要outDir 。使用 SystemJS 等工具( plugin-typescript )或 Webpack (使用 awesome-typescript-loader ts-loader )来托管TypeScript转换器。如果您使用 TS节点,则同样适用。

1: --outDir is not necessary if you additionally specify the --noEmit flag. You would do this when using a tool such as SystemJS (with plugin-typescript) or Webpack (with awesome-typescript-loader or ts-loader) to host the TypeScript transpiler. The same applies if you are using TS Node.

2 :虽然称为 JSDoc 评论,但它们的解释是在TypeScript类型系统, JSDoc系统。 TypeScript和Google的 Closure Compiler等语言和工具,有效地劫持JSDoc语法用于他们自己的目的,从而赋予其构造潜在的冲突含义。这通常不是问题,但值得了解,因为很难确定这些注释的适用性和正确性以及它们引用或声明的类型的兼容性。

2: Although called JSDoc comments, they are interpreted in the context of the TypeScript type system, not the JSDoc system. Languages and tools like TypeScript, and Google's Closure Compiler, effectively hijack the JSDoc syntax for their own purposes and thereby confer potentially conflicting meanings to its constructs. This isn't usually a problem but it's worth knowing because it can be difficult to determine the applicability and correctness of these comments and the compatibility of the types that they reference or declare.

备注:

虽然这个问题和答案都是关于导入用于JavaScript文件的类型,但由于编译器将会经常这样做从表达式的值推断出类型。

Although this question and answer is all about importing types for use in JavaScript files, it's very often unnecessary as the compiler will infer the types from the values of your expressions.

值得一提的是,如果你发现自己需要编写大量JSDoc样式类型的注释,那么你几乎肯定会更好将文件转换为TypeScript作为在JSDoc中表达类型的语法是笨拙的。感谢 - allowJs 选项,您可以逐个文件地执行此操作,如上所述。

It's also worth mentioning that if you find yourself needing to write a lot of JSDoc style type annotations you are almost certainly better off converting the file to TypeScript as the syntax for expressing types in JSDoc is clumsy. Thanks to the --allowJs option, you can do this on a file by file basis, as described above.

这篇关于在Node.js中混合使用JavaScript和TypeScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆