Why do I need to add the .js extension when importing files in typescript but not in Angular imports?

Art

In my typescript project I need to add the *.js extension when doing imports, otherwise the project will build but it will fail at runtime in the browser because it cannot find the file.

This is what my typescript imports look like:

import {MainApp} from './MainApp.js';

window.onload = () => {
  var app = new MainApp(5);
  app.doSomething();

};

From what I have read (Appending .js extension on relative import statements during Typescript compilation (ES6 modules) for example) it seems a normal thing for typescript that I cannot do this: import {MainApp} from './MainApp.js';

But the thing is that in Angular using typescript I can do this:

import {MainApp} from './MainApp';

So, how it is Angular doing it? There is a way I can replicate that behavior in my non angular, pure typescript project?

RaiderB

Because angular cli first compiles all your source files into fewer files for the browser. All your code from multiple files lands in just one .js file. At compile time the compiler finds the MainApp related file and puts it into the output file.

Whereas the typescript compiler for the most part just removes the TS parts and keeps the TS parts. Otherwise it doesn't touch the files. The browser then at runtime requests all the source .js files.

If you don't want to care about file endings in import you'll need a bundler. There are many different ones like webpack, rollup, parcel, and many more.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why do I need to import Combine when SwiftUI already imports it?

From Dev

why require need .js extension while importing js file

From Dev

Why do I need to add the 'run' argument when building an angular app (npm run build)?

From Dev

Do I need to opt-in to Ivy to use dynamic imports when lazy loading routes (Angular 8)?

From Dev

Why do i need to add markForCheck() to trigger change detection in Angular?

From Dev

Why do I need to include .o files when compiling?

From Dev

Why do script tags need to be left empty when importing a JS file?

From Dev

Why do I need to introduce the arguments when importing the functions of one file

From Dev

Typescript - How do I add an extension method

From Dev

Do imports carry over when importing a script

From Dev

Why do I need typings to work with typescript?

From Dev

Why do I need to type cast in Typescript?

From Dev

TypeScript is compiling into this failing JS file when importing a module. Why?

From Dev

Why do I need to specify 'from files'?

From Dev

TypeScript and Redux: Why do I need to add ` | undefined` to my Reducer state type?

From Dev

Why do I need to add .isToggleOn?

From Dev

When do I need to #include .cpp files?

From Dev

Angular: Is it possible for typescript imports auto locate files?

From Dev

Why do I need mark for check ? angular

From Dev

Why do I need add the folder when I get FileInputStream from a file?

From Dev

Why do I need git add when I use git commit -a

From Dev

Why do I need to supply a language version when parsing a TypeScript source file?

From Dev

When do imports need to include the path?

From Dev

Do I need to duplicate my Entity Framework classes in Typescript when using Angular 2?

From Dev

When and why do I need to quote asterisks

From Dev

Why do I need to angular.bootstrap even when I declare ng-app="MyApp" in JSFiddle

From Dev

Why do I need valid HTML-files as fragment container when running sonarqube?

From Dev

Why do I get circular import error when importing view?

From Dev

Why do I get a Swift compiler error when importing Vapor?

Related Related

  1. 1

    Why do I need to import Combine when SwiftUI already imports it?

  2. 2

    why require need .js extension while importing js file

  3. 3

    Why do I need to add the 'run' argument when building an angular app (npm run build)?

  4. 4

    Do I need to opt-in to Ivy to use dynamic imports when lazy loading routes (Angular 8)?

  5. 5

    Why do i need to add markForCheck() to trigger change detection in Angular?

  6. 6

    Why do I need to include .o files when compiling?

  7. 7

    Why do script tags need to be left empty when importing a JS file?

  8. 8

    Why do I need to introduce the arguments when importing the functions of one file

  9. 9

    Typescript - How do I add an extension method

  10. 10

    Do imports carry over when importing a script

  11. 11

    Why do I need typings to work with typescript?

  12. 12

    Why do I need to type cast in Typescript?

  13. 13

    TypeScript is compiling into this failing JS file when importing a module. Why?

  14. 14

    Why do I need to specify 'from files'?

  15. 15

    TypeScript and Redux: Why do I need to add ` | undefined` to my Reducer state type?

  16. 16

    Why do I need to add .isToggleOn?

  17. 17

    When do I need to #include .cpp files?

  18. 18

    Angular: Is it possible for typescript imports auto locate files?

  19. 19

    Why do I need mark for check ? angular

  20. 20

    Why do I need add the folder when I get FileInputStream from a file?

  21. 21

    Why do I need git add when I use git commit -a

  22. 22

    Why do I need to supply a language version when parsing a TypeScript source file?

  23. 23

    When do imports need to include the path?

  24. 24

    Do I need to duplicate my Entity Framework classes in Typescript when using Angular 2?

  25. 25

    When and why do I need to quote asterisks

  26. 26

    Why do I need to angular.bootstrap even when I declare ng-app="MyApp" in JSFiddle

  27. 27

    Why do I need valid HTML-files as fragment container when running sonarqube?

  28. 28

    Why do I get circular import error when importing view?

  29. 29

    Why do I get a Swift compiler error when importing Vapor?

HotTag

Archive