TypeScript: what is the difference between export default and export and when to use which?
Hi everyone! I recently started learning TypeScript (previously wrote in pure JS) and got confused about exports. There seem to be two ways: `export default` and just `export`. But I don't understand when to use which.
Here's an example. I have a file `user.ts`:
```typescript
// user.ts
export default class User {
constructor(public name: string) {}
}
export function formatUser(user: User): string {
return `User: ${user.name}`;
}
```
And in another file I import:
```typescript
// app.ts
import User from './user'; // this works
import { formatUser } from './user'; // this also works
```
But I've seen some people write `import * as UserModule from './user'`. Also, you can import default without curly braces, but named imports only with curly braces. Why is that?
And the main q
No answers yet.
Log in to leave an answer