Skip to main content

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

  1. Create a Word document named input.docx, type {{field}} and save it in the project directory.
input.docx
{{field}}
  1. 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');
  1. In the same project directory, create a file named tsconfig.json and copy-paste:
{
"compilerOptions": {
"module": "esnext",
"moduleResolution": "node",
"target": "es2017"
}
}
  1. Open package.json with a text editor and add a type:
{
...
"type": "module",
...
}
  1. Open a terminal window in this project directory and run:
Terminal

node --loader ts-node/esm index.ts

  1. output.docx has been generated and the {{field}} placeholder has been replaced with Anything you see fit.
output.docx

Anything you see fit