Scenario
We currently require fs
like this in node:
const fs = require('fs');
We are wondering what the equivalent typescript version is?
Answer
With Typescript, after running npm install --save-dev @types/node
we can import the fs
the specific function we want from the fs
library with a destructured import like this:
import { writeFileSync } from ‘fs';
The destructured part is important in the event that you want to minimize the module you are writing with tools like Webpack / Rollup. Since we are now only importing what we need, the remaining parts of the library we are importing from can be shaved off.
Looking for a more elaborate example. Checkout Using Papaparse With Typescript.