1

Working on react application in TypeScript.

I'm trying to understand the correct syntax for exporting a default component using as

Up till now I've used:

class HomePage extends Component<IProps, IState> {
   ...
}

export default HomePage

or in shortly:

export default class HomePage extends Component<IProps, IState> {
   ...
}

now, after adding redux support. I have something like:

class HomePage extends Component<IProps, IState> {
   ...
}

const mapStateToProps = (state: StoreState) => {
   ...
};

const mapDispatchToProps = {
   ...
};

export default connect(mapStateToProps, mapDispatchToProps)(HomePage);

but I can't figure it out how to add as to the connect default export e.g something like:

// This is NOT working. I get Syntax error.
export default connect(mapStateToProps, mapDispatchToProps)(HomePage) as HomePage;

is it possible? or should I do it? it's seems that anyway the export name is HomePage (when I'm importing HomePage in other file, the IDE is automatically detects and import this file). Where is it taken from? the name of the file (HomePage.tsx)?

This is do work for me:

const connectedHomePage = connect(mapStateToProps, mapDispatchToProps)(HomePage)
export {connectedHomePage as HomePage};

but it's two lines (I prefer in one line) and it's not a default export.

so, is there a way to export default AND adding as to the statement? just so it will be clear what is the exported name?

4
  • export default always exports as, well, default. What are you expecting to get with this syntax? Commented Jan 14, 2020 at 6:14
  • @Cerberus to understand how to make sure that when I import this from another file the IDE will recognize this export as HomePage and will be able to auto-import it (which it actually does!) I understood why IDE can recognize export default class HomePage as HomePage (import HomePage from './HomePage') but wasn't sure how it can also work for export default connect(mapStateToProps, mapDispatchToProps)(HomePage); when I write in another file HomePage the IDE can find it and auto import it the same. so I guess maybe it's know which default I want to import by the filename? Commented Jan 15, 2020 at 13:48
  • It seems to depend on IDE, then, but probably it can either use the filename or recognise some common idioms. Is it required in your case to use export default .. and not e.g. export const HomePage = ...? Commented Jan 15, 2020 at 16:10
  • @Cerberus thank you. no it's only as I'm curious on how things works, and as import HomePage from './HomePage' looks better than import {HomePage} from './HomePage' Commented Jan 15, 2020 at 20:13

1 Answer 1

7

as has 2 meanings. In ES6 modules, it can be a way to rename an import or export. In TypeScript, it is a typecast operator. I am going to assume you mean the first, specifically a way to rename an export.

Now take a look at this expression you suggested:

export default connect(mapStateToProps, mapDispatchToProps)(HomePage) as HomePage;

Do you want to export it as default or do you want to export it as HomePage? When you export default, default is the export name. Given that, does this make sense to expect it to be called HomePage? You either export something that is called default or something that is called HomePage.

This table associates different export statement forms with the expected export name and local names:

enter image description here

For importing, though...

  • If you are importing a default export, you have to explicitly name the import. Default exports will not automatically have a local name after importing. Example: import express from "express". The local name does not matter. Could as well be import banana from "express".
  • If you are importing a named export, by default, the local name will be the same as the exported name. Example: If you have export const x = 2, then, you should import it as import { x } from "module". You can also rename the import like this: `import { x as y } from "module".

enter image description here

The above tables are from the ES6 specs: http://www.ecma-international.org/ecma-262/6.0/

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

3 Comments

Thank you for the detailed & enriching information. What I don't understand is - when you export as default (export name is "default") when the IDE auto-import it to another file, how it's import it as HomePage (e.g import HomePage from './HomePage') and not as default (import default from './HomePage'). is default export always imported by the IDE as the filename?
@ET-CS, I added a second part to the answer, in which I explain how imports work. Basically, for default exports, you name it as you wish, when you import them.
shame there is no export * as v from mod

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.