Ole
Ole

Reputation: 47038

Difference between default and regular typescript exports?

Just want to see whether I understand the difference between typescript exports that use the default keyword and 'normal' exports. For example:

import validate from "./StaticZipCodeValidator";

In this case we don't need to to surround validate with curly {} braces, since it's the default export of "./StaticZipCodeValidator";

So when exporting defaults we don't use curly braces as shown here:

declare let $: JQuery;
export default $;

And when importing we also don't use curly braces. Did I miss anything?

Upvotes: 0

Views: 103

Answers (3)

Leonardo Venoso
Leonardo Venoso

Reputation: 1222

Let's say you have a file (module). A module can have multiple clases, abstractions, and interfaces.

Then, when you import a non default class, abstractions, or interface you have to use the curly braces, otherwise you import de default exported component.

import {NotDefaultClass} from "DefaultExportedClass";

import DefaultExportedClass from "DefaultExportedClass";

Upvotes: 0

Paleo
Paleo

Reputation: 23702

TypeScript modules are an implementation of ES6 modules. I suggest the ES6 introduction to modules from Mozilla:

import _ from "lodash";

This shorthand is equivalent to import {default as _} from "lodash";.

[...] There’s nothing magic about a default export; it’s just like any other export, except it’s named "default".

The member default is intended to replace the CommonJS module.exports =. It is better because we keep the ability to export additional things later.

Upvotes: 1

basarat
basarat

Reputation: 275987

And when importing we also don't use curly braces. Did I miss anything?

No.

That said. Personally I don't use default exports for various reasons. I have seen other OSS libraries making the choice to avoid this feature as well.

Upvotes: 1

Related Questions