Deno-将TypeScript导入JS文件 [英] Deno - Importing TypeScript into a JS file

查看:186
本文介绍了Deno-将TypeScript导入JS文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Deno中,要导入TypeScript模块,您自己的代码文件是否必须为TypeScript?还是Deno在导入模块之前自动将TypeScript转换为javascript?

In Deno, to import a TypeScript module, does your own code file have to be TypeScript? Or does Deno auto convert TypeScript to javascript before the module gets imported?

我希望我所有的代码文件都是EcmaScript模块(js或mjs,但不是ts).

I want all my code files to be EcmaScript modules (js or mjs, but not ts).

与如今的其他所有人不同,我想避免在自己的代码中使用TypeScript.我不喜欢静态类型的严格性,并且Typescript不是EcmaScript标准的一部分.我只有EcmaScript才能管理大型项目.对我而言,TypeScript是一种过时的技术,自ES6模块问世以来就不再需要. TypeScript解决的问题类型是我没有的问题.

Unlike everyone else these days, I want to avoid using TypeScript in my own code. I dislike the rigidity of static types and Typescript is not part of the EcmaScript standard. EcmaScript alone has all I need to manage big projects. To me, TypeScript is an antiquated technology that has not been necessary since the advent of ES6 modules. The types of problems TypeScript addresses are problems I do not have.

但是,很明显,许多Deno中可用的第三方模块都将用TypeScript编写.是否可以采用在导入之前进行到JavaScript转换的方式来导入那些模块,以便我可以纯粹将自己的代码保留为EcmaScript?

However, clearly, many 3rd party modules available in Deno, will be written in TypeScript. Is it possible to import those modules in a manner where the conversion to JavaScript happens before the import, so that I can keep my own code purely EcmaScript?

推荐答案

您可以使用JavaScript编写自己的代码.

You can write your own code with JavaScript.

假设您已经或正在使用TypeScript文件/模块numbers.ts:

Suppose you have or are using a TypeScript file/module numbers.ts:

export function isEven(n: number): boolean {
    if (n % 2 != 0) {
        return false
    }
    return true;
}

您可以使用app.js JavaScript脚本导入并运行它:

You can import and run it with an app.js JavaScript script:

import { isEven } from "./module.ts";

const one = isEven(1)
const two = isEven(2)

console.log(one)
console.log(two)

Deno在内部将TypeScript转换为JavaScript.使用标准库或第三方库时,过程相同. Deno项目的成员通过将其作为目标而走得更远:

Deno does the TypeScript convertion to JavaScript internally. The process is the same when using standard or 3rd party libraries. The folks at the Deno project went even further by adding it as a goal:

https://deno.land/manual/introduction

与浏览器兼容:编写的Deno程序的子集 完全使用JavaScript,并且不要使用全局Deno命名空间(或 功能测试),还应该能够在现代网络中运行 浏览器无需更改.

Browser compatible: The subset of Deno programs which are written completely in JavaScript and do not use the global Deno namespace (or feature test for it), ought to also be able to be run in a modern web browser without change.

名称解析必须完全合格.在这个使用TypeScript的专用页面中,还有很多有关引用类型定义的信息:

Name resolution must be fully qualified. There's a whole lot more about referencing type definitions in this dedicated page for using TypeScript:

https://deno.land/manual/getting_started/typescript

Deno同时支持JavaScript和TypeScript作为一流语言 在运行时.这意味着它需要标准的模块名称, 包括扩展名(或提供正确媒体类型的服务器)

Deno supports both JavaScript and TypeScript as first class languages at runtime. This means it requires fully qualified module names, including the extension (or a server providing the correct media type)

示例:

import { config } from "https://deno.land/x/dotenv/mod.ts";

按照上面的示例,您可以使用bundle命令生成具有所有依赖项的单个JavaScript文件.捆绑它将获取我的app.jsmodule.ts文件,并创建一个新的app.bundle.js文件,即JavaScript.

Following my example above you can use the bundle command to generate a single JavaScript file with all the dependencies. Bundling it will take my app.js and module.ts files and create a new file app.bundle.js which is JavaScript.

https://deno.land/manual/tools/bundler

$ deno bundle app.js app.bundle.js
Bundling file:///home/pomatti/projects/deno-sandbox/app.js
Emitting bundle to "app.bundle.js"
3111 bytes emmited.
$ deno run app.bundle.js
false
true

它甚至可以在浏览器中加载:

It can even be loaded in the browser:

捆绑包也可以加载到Web浏览器中.捆绑包是一个 独立的ES模块,因此必须将type的属性设置为 模块".例如:

Bundles can also be loaded in the web browser. The bundle is a self-contained ES module, and so the attribute of type must be set to "module". For example:

<script type="module" src="website.bundle.js"></script>

对于ECMAScript模块,我想指出TypeScript也可以实现它.

As for ECMAScript modules I would like to point out that TypeScript implements it as well.

https://github.com/microsoft/TypeScript/issues/2242

https://www.staging-typescript.org/docs/handbook/modules.html

从ECMAScript 2015开始,JavaScript具有模块的概念. TypeScript拥有这个概念.

Starting with ECMAScript 2015, JavaScript has a concept of modules. TypeScript shares this concept.

现在,静态类型"讨论不在本论坛讨论范围之内,因此我在这里不予赘述,但我相信我涵盖了所有其他内容.

Now, the "static type" discussion falls out of scope of this forum so I won't touch it here, but I believe I covered everything else.

这篇关于Deno-将TypeScript导入JS文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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