mirror of
https://github.com/jbranchaud/til
synced 2026-01-03 07:08:01 +00:00
Add Re-Export An Imported Type as a TypeScript til
This commit is contained in:
@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
|
||||
|
||||
For a steady stream of TILs, [sign up for my newsletter](https://tinyletter.com/jbranchaud).
|
||||
|
||||
_1042 TILs and counting..._
|
||||
_1043 TILs and counting..._
|
||||
|
||||
---
|
||||
|
||||
@@ -50,6 +50,7 @@ _1042 TILs and counting..._
|
||||
* [Shell](#shell)
|
||||
* [Tailwind CSS](#tailwind-css)
|
||||
* [tmux](#tmux)
|
||||
* [TypeScript](#typescript)
|
||||
* [Unix](#unix)
|
||||
* [Vercel](#vercel)
|
||||
* [Vim](#vim)
|
||||
@@ -944,6 +945,10 @@ _1042 TILs and counting..._
|
||||
- [tmux in your tmux](tmux/tmux-in-your-tmux.md)
|
||||
- [Toggle Between Two Common Sessions](tmux/toggle-between-two-common-sessions.md)
|
||||
|
||||
### TypeScript
|
||||
|
||||
- [Re-Export An Imported Type](typescript/re-export-an-imported-type.md)
|
||||
|
||||
### Unix
|
||||
|
||||
- [All The Environment Variables](unix/all-the-environment-variables.md)
|
||||
|
||||
31
typescript/re-export-an-imported-type.md
Normal file
31
typescript/re-export-an-imported-type.md
Normal file
@@ -0,0 +1,31 @@
|
||||
# Re-Export An Imported Type
|
||||
|
||||
I have a TypeScript module that is defining an XState machine. Among other
|
||||
things, it imports the `DoneEventObject` type from `xstate`. I want to
|
||||
re-export that type so that any modules using this machine have access to that
|
||||
type definition.
|
||||
|
||||
This can be done a couple of ways. One way to import it under an aliased name
|
||||
and then assign + export it using the original name.
|
||||
|
||||
```typescript
|
||||
import {Machine, DoneEventObject as _DoneEventObject} from 'xstate'
|
||||
|
||||
export type DoneEventObject = _DoneEventObject
|
||||
```
|
||||
|
||||
This works, but adds some potential indirection and confusion through the
|
||||
double assignment.
|
||||
|
||||
Another way of doing this is to reference the type off the import statement as
|
||||
part of an assignment.
|
||||
|
||||
```typescript
|
||||
import {Machine} from 'xstate'
|
||||
|
||||
export type DoneEventObject = import('xstate').DoneEventObject
|
||||
```
|
||||
|
||||
This imports, assigns, and exports the type in a single statement.
|
||||
|
||||
[source](https://github.com/microsoft/TypeScript/issues/28481#issuecomment-552938424)
|
||||
Reference in New Issue
Block a user