1

I'm not able to find out a good description of tpescript imports.

What are the difference between this import types?

  • import { Country } from '../../models/country.model.ts'; This seems normal. You import an exported class/interface of a project ts file

  • import * as mapsData from 'blablabla/world.js'; In this case am I referenceing a js file directly? Is this possible? Are there some preconditions? This works without using the allowJs flag.

  • import something from 'path' Why is this case I'm not using brackets?

2
  • you don't need incoude file extensions. The trick is to include allowJS in tsconfig.json file. The default option is false. Refer to: typescriptlang.org/docs/handbook/compiler-options.html Commented Mar 15, 2018 at 9:46
  • import something from 'path' mean this module has default export. Without './path', you refer to module rather than local files. Commented Mar 15, 2018 at 9:48

1 Answer 1

2

I will try to explain using examples

Let's assume we have 2 files, exp.ts and imp.ts.

// exp.ts
export const A = 1;
export default 2;

// imp.ts
import {A} from "./exp";
import * as B from "./exp";
import C from "./exp";

console.log(A)
console.log(B)
console.log(C)

Executing imp.ts will give result

1
{ A: 1, default: 2 }
2

Why?

import {A} from "./exp"; - imports named element as a variable

import * as B from "./exp"; - imports all exported elements and saves them to a variable

import C from "./exp"; - imports default export and saves it to a variable, this is equivalent of import {default as C} from "./exp";

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.