Skip to content

Commit 9b7aacc

Browse files
committed
Add Re-Export An Imported Type as a TypeScript til
1 parent 58e2b32 commit 9b7aacc

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

README.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
1010

1111
For a steady stream of TILs, [sign up for my newsletter](https://tinyletter.com/jbranchaud).
1212

13-
_1042 TILs and counting..._
13+
_1043 TILs and counting..._
1414

1515
---
1616

@@ -50,6 +50,7 @@ _1042 TILs and counting..._
5050
* [Shell](#shell)
5151
* [Tailwind CSS](#tailwind-css)
5252
* [tmux](#tmux)
53+
* [TypeScript](#typescript)
5354
* [Unix](#unix)
5455
* [Vercel](#vercel)
5556
* [Vim](#vim)
@@ -944,6 +945,10 @@ _1042 TILs and counting..._
944945
- [tmux in your tmux](tmux/tmux-in-your-tmux.md)
945946
- [Toggle Between Two Common Sessions](tmux/toggle-between-two-common-sessions.md)
946947

948+
### TypeScript
949+
950+
- [Re-Export An Imported Type](typescript/re-export-an-imported-type.md)
951+
947952
### Unix
948953

949954
- [All The Environment Variables](unix/all-the-environment-variables.md)
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Re-Export An Imported Type
2+
3+
I have a TypeScript module that is defining an XState machine. Among other
4+
things, it imports the `DoneEventObject` type from `xstate`. I want to
5+
re-export that type so that any modules using this machine have access to that
6+
type definition.
7+
8+
This can be done a couple of ways. One way to import it under an aliased name
9+
and then assign + export it using the original name.
10+
11+
```typescript
12+
import {Machine, DoneEventObject as _DoneEventObject} from 'xstate'
13+
14+
export type DoneEventObject = _DoneEventObject
15+
```
16+
17+
This works, but adds some potential indirection and confusion through the
18+
double assignment.
19+
20+
Another way of doing this is to reference the type off the import statement as
21+
part of an assignment.
22+
23+
```typescript
24+
import {Machine} from 'xstate'
25+
26+
export type DoneEventObject = import('xstate').DoneEventObject
27+
```
28+
29+
This imports, assigns, and exports the type in a single statement.
30+
31+
[source](https://github.com/microsoft/TypeScript/issues/28481#issuecomment-552938424)

0 commit comments

Comments
 (0)