3

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.

2 Answers 2

6

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.

Sign up to request clarification or add additional context in comments.

3 Comments

"You can't import typescript files into javascript files, but you can do it vice versa." Not agree with your point @masserbill, we can use typescript in our javascript file. Everything is possible in JS world.
@AzeemChauhan Everything is possible in JS world.. Definitley not true. As long as you make not use of tsc or babel you will not be able to parse typescript code. This is a simple fact.
I mostly agree, but your note ("You can't import typescript files into javascript files, but you can do it vice versa") can be a bit misleading. While it's true that you cannot import TS code with "vanilla" Node, the tools you mention enable one to do so.
0

You can do that through babel. Follow below article

Article: How to use typescript file in javascript project

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.