0

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?

3 Answers 3

1

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.

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

Comments

1

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.

Comments

0

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";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.