Skip to content

Commit 46d121c

Browse files
committed
Add optional chaining, discussion on named imports
1 parent f816fe6 commit 46d121c

File tree

7 files changed

+56
-13
lines changed

7 files changed

+56
-13
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
*.js

Lib.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
export class Lib {
2-
run() {
3-
console.log('Success!');
2+
run(): void {
3+
// Make sure optional chaining works - either by `tsc` transpiling it, or by node running it under a flag
4+
const a = {
5+
b: {
6+
c: 'foo'
7+
}
8+
};
9+
a.b?.c && console.log('Success!');
410
}
511
}

README.md

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ Minimalistic example of configuring TypeScript and Node to:
77
* import your own modules without specifying an extension
88
* run the resulting JavaScript code
99

10+
There is no need for Babel.
11+
1012
# Emit ES modules code
1113

1214
In `tsconfig.json`, set this in `compilerOptions`:
@@ -16,23 +18,42 @@ In `tsconfig.json`, set this in `compilerOptions`:
1618
"module": "esnext", // Output `import`/`export` ES modules
1719
```
1820

21+
1922
# Import modules that use Node built-ins (`http`, `url` etc.)
2023

2124
* run `npm install --save-dev @types/node`
2225
* in `tsconfig.json` under `compilerOptions`, set
2326
* `"moduleResolution": "node"`, so `tsc` can find modules [when targeting ES6+](https://github.com/Microsoft/TypeScript/issues/8189)
2427
* `"types": ["node"]` to avoid errors related to Node built-in modules
2528

29+
2630
# Import modules that don't have named exports
2731

28-
Set `"allowSyntheticDefaultImports": true` in `tsconfig.json`. In our code,
29-
instead of `import { InfluxDB } from 'influx` we have to write:
32+
Normally we could write in TypeScript
33+
34+
import { InfluxDB } from 'influx';
35+
36+
but when generating ES modules code, that statement will be passed through as is, and will cause Node to fail with
37+
38+
> SyntaxError: The requested module 'influx' does not provide an export named 'InfluxDB'
39+
40+
because [`node-influx` doesn't provide named exports](https://github.com/node-influx/node-influx/issues/298) (and neither does an even more popular module, [`apollo-server`](https://github.com/apollographql/apollo-server/issues/1356#issuecomment-565277759)).
41+
42+
One alternative would be to generate old ugly commonjs modules code by,
43+
44+
* removing the `"type": "module"` line from `package.json`, and
45+
* changing the module line to `"module": "CommonJS"` in `tsconfig.json` (`allowSyntheticDefaultImports` also becomes unnecessary)
46+
47+
Another is to import the entire module:
3048

3149
```js
3250
import Influx from 'influx';
3351
const influx = new Influx.InfluxDB();
3452
```
3553

54+
However, this will generate `Error TS1192: Module '...' has no default export.` To prevent that, set `"allowSyntheticDefaultImports": true` in `tsconfig.json`.
55+
56+
3657
# Import your own modules without specifying an extension
3758

3859
When transpiling, [TypeScript won't generate an extension for you](https://github.com/microsoft/TypeScript/issues/16577). Run Node with the `node --experimental-specifier-resolution=node` parameter:
@@ -41,12 +62,16 @@ When transpiling, [TypeScript won't generate an extension for you](https://githu
4162

4263
Otherwise, [node mandates that you specify the extension](https://nodejs.org/api/esm.html#esm_mandatory_file_extensions) in the `import` statement.
4364

65+
To support optional chaining, add the `--harmony` flag to the node command line.
66+
67+
4468
# Run the resulting JavaScript code
4569

4670
Add `"type": "module"` to `package.json`, because [TypeScript can't generate files with the .mjs extension](https://github.com/microsoft/TypeScript/issues/18442#issuecomment-581738714).
4771

72+
4873
# ESLint
4974

50-
To be able to run `eslint`, we must create an `.eslintrc.cjs` file, rather than a `.js` one. Then, install the required dependencies:
75+
To be able to run `eslint`, we must create an `.eslintrc.cjs` file, rather than a `.js` one (due to `"type": "module"` in `package.json`). Then, install the required dependencies:
5176

5277
npm i -D eslint @typescript-eslint/eslint-plugin @typescript-eslint/parser

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
"type": "module",
1616
"main": "run.js",
1717
"scripts": {
18-
"start": "node --experimental-specifier-resolution=node run.js",
18+
"build": "rm *.js && tsc",
19+
"start": "node --experimental-specifier-resolution=node --harmony run.js",
1920
"lint": "eslint *.ts"
2021
},
2122
"dependencies": {
@@ -26,6 +27,6 @@
2627
"@typescript-eslint/eslint-plugin": "^2.19.0",
2728
"@typescript-eslint/parser": "^2.19.0",
2829
"eslint": "^6.8.0",
29-
"typescript": "^3.7.5"
30+
"typescript": "^3.8.3"
3031
}
3132
}

run.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1+
// Import without specifying the extension. `./Lib.js` would look odd in a .ts file
12
import { Lib } from './Lib';
3+
4+
// Can't use named imports because https://github.com/node-influx/node-influx/issues/298
5+
// import { InfluxDB } from 'influx';
6+
// const influx = new InfluxDB();
7+
28
import Influx from 'influx';
39
const influx = new Influx.InfluxDB();
10+
411
const l = new Lib();
512
l.run();
13+
console.log(typeof influx);

tsconfig.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
{
22
"compilerOptions": {
33
"target": "esnext",
4-
"module": "esnext", // Output `import`/`export` ES modules
5-
"moduleResolution": "node", // Find modules in node_modules - https://github.com/Microsoft/TypeScript/issues/8189#issuecomment-212079251
6-
"allowSyntheticDefaultImports": true, // Allow default imports from modules with no default export. This does not affect code emit, just typechecking.
4+
"module": "esnext", // output `import`/`export` ES modules
5+
//"module": "CommonJS", // enables `import { InfluxDB } from 'influx'
6+
"allowSyntheticDefaultImports": true, // allow default imports from modules with no default export. This does not affect code emit, just typechecking.
7+
"moduleResolution": "node", // find modules in node_modules - https://github.com/Microsoft/TypeScript/issues/8189#issuecomment-212079251
78
"types": ["node"] // Influx uses built-in node modules like http or url
89
}
910
}

0 commit comments

Comments
 (0)