TypeScript Tutorial
The previous nodeJS tutorial, but in TypeScript.
Prerequisites
Download and install nodeJS v16+ from https://nodejs.org/.
Tip
run node --version
from a terminal window to confirm installation.
Installation
Create a project directory, make it your working directory, and run from a terminal window:
Terminal
npm init -y
npm i typescript
npm i ts-node
npm i @yumdocs/yumdocs
Tip
Besides transpilers, an alternative to ts-node on top of NodeJS, is Deno.
Also note that you do not have to install type declarations, which are packaged with @yumdocs/yumdocs.
Getting started
- Create a Word document named
input.docx
, type{{field}}
and save it in the project directory.
input.docx
{{field}}
- In the same project directory, create a file named
index.ts
and copy-paste:
import {YumTemplate} from '@yumdocs/yumdocs';
const t = new YumTemplate();
await t.load('./input.docx');
await t.render({field: 'Anything you see fit'});
await t.saveAs('./output.docx');
- In the same project directory, create a file named
tsconfig.json
and copy-paste:
{
"compilerOptions": {
"module": "esnext",
"moduleResolution": "node",
"target": "es2017"
}
}
- Open
package.json
with a text editor and add a type:
{
...
"type": "module",
...
}
- Open a terminal window in this project directory and run:
Terminal
node --loader ts-node/esm index.ts
output.docx
has been generated and the{{field}}
placeholder has been replaced withAnything you see fit
.
output.docx
Anything you see fit