marvin
By Marvin Desmond
Posted 29th March 2023

TypeScript Configuration

Let's configure the code in Modules and Packages for JS to work with TypeScript
At the moment, the directory structure for the root file fourier, is as below
            $ ls *
main.js         package.json

hello:
hello.js
$
          
The code files are as below, in the file hello.js in the folder hello in root, fourier:
const os = require('os')

// create anonymous function and bind to variable SayArch
const SayArch = () => {
  let arch = "OS info: \n"
  arch += `Type ${os.type()} ` +
            `Release: ${os.release()} ` +
            `Platform: ${os.platform()}`
  return arch
}

module.exports = { SayArch }
In the root file, main.js in the root folder, fourier:
'use strict';

const { SayArch } = require('./hello/hello.js')

console.log("Hello World")
let arch = SayArch()
console.log(arch)
The package.json, which acts as the versioning for the third-party packages, in its current state, exists as below:
            {
"name": "fourier",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
    "start": "node main.js",
    "test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
          
So, let's download the packages needed for node for TypeScript
And initialize the config file for TypeScript, tsconfig.json
Note: Any package can be searched from npm.js, npmjs.com and installed using the syntax npm install PACKAGE
js
npm install typescript ts-node @types/node|
The resulting execution for the terminal becomes:
sh
            $ npm install typescript ts-node @types/node

added 19 packages, and audited 20 packages in 39s

found 0 vulnerabilities
$ ls
hello                   node_modules            package.json
main.js                 package-lock.json
$ npx tsc --init

Created a new tsconfig.json with:                                                  
                                                                                TS 
target: es2016
module: commonjs
strict: true
esModuleInterop: true
skipLibCheck: true
forceConsistentCasingInFileNames: true


You can learn more at https://aka.ms/tsconfig
$
          
The final part of the configuration is to rename all js files to ts. With this done. There's a final change on the start script of package.json. Let ts-node from the local project module run the program. By local I mean prefixing npx
            ...
"scripts": {
    "start": "npx ts-node main.ts",
    "test": "echo \"Error: no test specified\" && exit 1"
},
...
          
Note: I'm going to omit listing files in node_modules directory
Thus, the directory structure for the project with TypeScript configured becomes:
            $ find . -not -path "./node_modules/*" 
.
./main.ts
./node_modules
./package-lock.json
./package.json
./tsconfig.json
./hello
./hello/hello.ts
$
          
The codebase for main.ts becomes
'use strict';

const { SayArch } = require('./hello/hello.ts')

console.log("Hello World")
let arch = SayArch("OS Info ==> ")
console.log(arch)
And the codebase for ./hello/hello.ts becomes
const os = require('os')

const SayArch = (header: string):string => {
  let arch = header
  arch += `Type ${os.type()} ` +
            `Release: ${os.release()} ` +
            `Platform: ${os.platform()}`
  return arch
}

export { SayArch }
Now, finally onto running the project that has been created and configured in TypeScript
            $ npm start 

> fourier@1.0.0 start
> npx ts-node main.ts

Hello World
OS Info ==> Type Darwin Release: 21.6.0 Platform: darwin
$
          
That's it. We're done!