UNPKG

1.74 kBMarkdownView Raw
1# Typescript Transform Export Interop
2
3TypeScript transform for exporting a module that can be easily imported both from TypeScript and from Node.js.
4
5## Install
6
7```sh
8npm install --save-dev typescript-transform-export-interop
9```
10
11## Usage
12
13Ideally 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
17import Foo from 'foo';
18import * as Foo from 'foo';
19// Node.js
20const Foo = require ( 'foo' );
21```
22
23In 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
251. Export your modules as `export default Foo`, if you need multiple exports don't export a default one.
26
272. Add `tstei` to your compilation chain: `tsc && tstei`.
28
29## Notes
30
31#### Type Exporting
32
33Sometimes, 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
36import Foo, {type as FooType} from 'foo';
37
38class Bar {
39 foo: FooType
40}
41
42export default Bar;
43```
44
45Unfortunately there's no cleaner way of doing this while still supporting clean, interoperable, importing.
46
47#### Safety
48
49This 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
51In any case if this module detects that the transformation is not safe to make an error will be thrown.
52
53## License
54
55MIT © Fabio Spampinato