1 | # Typescript Transform Export Interop
|
2 |
|
3 | TypeScript transform for exporting a module that can be easily imported both from TypeScript and from Node.js.
|
4 |
|
5 | ## Install
|
6 |
|
7 | ```sh
|
8 | npm install --save-dev typescript-transform-export-interop
|
9 | ```
|
10 |
|
11 | ## Usage
|
12 |
|
13 | Ideally you should export your TypeScript modules in a way that importing them is a painless experience for your users, that means supporting all these kinds of imports:
|
14 |
|
15 | ```typescript
|
16 | // TypeScript
|
17 | import Foo from 'foo';
|
18 | import * as Foo from 'foo';
|
19 | // Node.js
|
20 | const Foo = require ( 'foo' );
|
21 | ```
|
22 |
|
23 | In order to do this your code must be exported in a particular, super-ugly, [way](https://github.com/Microsoft/TypeScript/issues/28335#event-1955245818), or you could just use this module:
|
24 |
|
25 | 1. Export your modules as `export default Foo`, if you need multiple exports don't export a default one.
|
26 |
|
27 | 2. Add `tstei` to your compilation chain: `tsc && tstei`.
|
28 |
|
29 | ## Notes
|
30 |
|
31 | #### Type Exporting
|
32 |
|
33 | Sometimes, if you'll export the type of a module exported using `typescript-transform-export-interop`, you'll have to explicitly import its type this way:
|
34 |
|
35 | ```typescript
|
36 | import Foo, {type as FooType} from 'foo';
|
37 |
|
38 | class Bar {
|
39 | foo: FooType
|
40 | }
|
41 |
|
42 | export default Bar;
|
43 | ```
|
44 |
|
45 | Unfortunately there's no cleaner way of doing this while still supporting clean, interoperable, importing.
|
46 |
|
47 | #### Safety
|
48 |
|
49 | This module uses regexes for transforming your export, instead of modifying the AST (there isn't a Babel equivalent for TypeScript declarations, is there?) so you should be careful not to write weird things like strings containing `export default Foo`, they will throw off this module.
|
50 |
|
51 | In any case if this module detects that the transformation is not safe to make an error will be thrown.
|
52 |
|
53 | ## License
|
54 |
|
55 | MIT © Fabio Spampinato
|