Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

201
Visualizações
Compile Typescript to run in the browser using modules

With Javascript I can use modules (i.e. import and export statements) and subdivide the code in different files and have such code run in the browser.

Let's take the simplest example made of 3 files: my-function-js.js, main-js.js and page-js.html

my-function-js.js

export function myFunctionJs() {
  console.log("here I am, a pure Javascript function");
}

main-js.js

import { myFunctionJs } from "./my-function-js.js";

myFunctionJs();

page-js.html

<!DOCTYPE html>
<html>
  <body>
    <script type="module" src="./main-js.js"></script>
  </body>
</html>

If I try to do the same with Typescript (the same code in files with the .ts extensions) I fail because the compiler generates all kinds of "module related" depending on the module option given to the compiler but is not able to simply maintain the code like it is and have it work like Javascript works with modules.

I realize this is a sort of theoretical question, that we should used bundlers and so on, but I was just curious to know if there is a simple way to make modules work in the browser just using the Typescript compiler.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

when running tsc on the theoretical code / files in your question, it generates the desired output which works fine in the browser, so long as you set the target in your tsconfig.json to es6 or higher, since the features you're trying to use were introduced in es6:

  "target": "es6",

the only trick of it though is that in your ts file you need to include the .js extension you're expecting to be generated in your import, or you need to include no extension and configure your web server to be able to figure out what the browser is asking for when it requests the file with no extension.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda