1

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?

str
  • 42,689
  • 17
  • 109
  • 127
Socrates
  • 8,724
  • 25
  • 66
  • 113

1 Answers1

2

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.

Estus Flask
  • 206,104
  • 70
  • 425
  • 565
  • 1
    I'll just add that if you want to import ilke `export default` does but the module doesn't have export default in Typescript strict mode, you'll have to provide Typescript with the `allowSyntheticDefaultImports` compiler option – Urigo Jan 17 '20 at 19:10