在Javascript中导入Typescript文件

在Javascript中导入Typescript文件

问题描述:

我正在做一个Chrome扩展程序,并且试图用Javascript导入Typescript文件. 这两个文件彼此相邻.

I'm making a Chrome extension and I'm trying to import a Typescript file in Javascript. 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不能直接从浏览器/node.js环境中执行.首先,您必须将其transpile转换为本机javascript.为此,您需要执行tsc.

TypeScript cannot be executed from a browser / node.js environment directly. At first you have to transpile it into native javascript. To do this you will need to execute tsc.

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

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

Typescript是JavaScript的所谓超集",这意味着所有打字稿代码都将从打字稿编译器转换为普通的JS代码.通常,这些编译过程与bundler链接,例如webpack.

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. Often those compile processes are linked with a bundler like webpack.

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

知道此错误The server responded with a non-JavaScript MIME type很有道理,因为打字稿文件是非JavaScript MIME类型".

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文件,但是反之亦然.

You can't import typescript files into javascript files, but you can do it vice versa.