Search code examples
javascripttypescriptmoduledefault

TypeScript default import vs non-default?


I'm having a hard time understanding the default keyword in TypeScript. So according to the TypeScript Documentation the default keyword can be placed on classes, functions, and variables. I though don't understand what the advantage with or without the default keyword is, when it i.e. comes to classes. When I import a non-default class, I might do something like this:

import { MyStuff } from './myStuff';

When I import a default class, I might do that:

import MyStuff from './myStuff';

But then, after the import, I make no difference between both import types, when using the imported class:

class MyClass {
  private myStuff: MyStuff[] = [];
}

Could someone please explain the use of the default keyword using a little code example?


Solution

  • There's no difference between default and named imports except import syntax.

    There's a difference between default and named exports. Named exports follow strict syntax and export variables, functions or classes. Default exports accept expressions.