简体   繁体   English

在 Javascript 中导入 Typescript 文件

[英]Import Typescript file in Javascript

I'm making a Chrome extension and I'm trying to import a Typescript file in Javascript.我正在制作一个 Chrome 扩展程序,我正在尝试用 Javascript 导入一个 Typescript 文件。 Both files are next to each others.这两个文件彼此相邻。

I'm importing it this way:我是这样导入的:

import { ApiParsing } from './ApiParsing.ts';

When I'm using the extension I have the following error:当我使用扩展程序时,出现以下错误:

Failed to load module script: The server responded with a non-JavaScript MIME type of "". Strict MIME type checking is enforced for module scripts per HTML spec.

TypeScript cannot be executed from a browser / node.js environment directly. TypeScript 不能直接从浏览器/node.js 环境执行。 At first you have to transpile it into native javascript.首先,你必须transpile成原生的JavaScript。 To do this you will need to execute tsc .为此,您需要执行tsc

https://www.typescriptlang.org/docs/handbook/compiler-options.html https://www.typescriptlang.org/docs/handbook/compiler-options.html

https://www.typescriptlang.org/docs/tutorial.html https://www.typescriptlang.org/docs/tutorial.html

Typescript is a so called "Superset" of JavaScript which means that all typescript code will be transpiled into usual JS code from the typescript compiler. Typescript 是所谓的 JavaScript 的“超集”,这意味着所有的 typescript 代码都将从 typescript 编译器转换为通常的 JS 代码。 Often those compile processes are linked with a bundler like webpack .通常这些编译过程与链接bundler喜欢webpack

https://webpack.js.org/concepts/ https://webpack.js.org/concepts/

Knowing this your error The server responded with a non-JavaScript MIME type makes a lot of sense due to a typescript file is a "non-JavaScript MIME type".知道这是你的错误由于打字稿文件是“非 JavaScript MIME 类型”, The server responded with a non-JavaScript MIME type是很有意义的。

Note:笔记:

You can't import typescript files into javascript files, but you can do it vice versa.您不能将打字稿文件导入 javascript 文件,但反之亦然。

You can do that through babel .你可以通过babel做到这一点。 Follow below article按照下面的文章

Article: How to use typescript file in javascript project 文章:如何在 javascript 项目中使用 typescript 文件

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

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