Import Typescript file in Javascript

一个人想着一个人 提交于 2020-03-05 04:05:19

问题


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.

回答1:


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 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/

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".

Note:

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



来源:https://stackoverflow.com/questions/54410351/import-typescript-file-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!