TypeScript

From LinuxReviews
Jump to navigationJump to search

TypeScript is a programming language consisting of a JavaScript superset. TypeScript is maintained by Microsoft. It can be used to create games and all sorts of applications without having to learn or use a real non-toy programming language like C.

written by 林慧 (Wai Lin) 2021-03-13 - last edited 2021-03-13. © CC BY

HOWTO "Compile" TypeScript programs[edit]

TypeScript source code is supposed to be converted to JavaScript .js files that are ran by a JavaScript interpreter like Node.js, they aren't really compiled in the traditional sense.

You will typically need Node.js and the npm package manager to "compile" and run TypeScript programs. You will also need to have TypeScript installed. It can either be installed by a distribution package that would typically be named nodejs-typescript or it can be installed using npm:

npm install -g typescript

The tsc command is used to compile TypeScript .ts files. You can check if you (already) have it installed by running which tsc (or just type tsc and see if you get its help output or command not found).

You will likely also want nodejs-tslib

All .ts files in a folder can be compiled with one command by running tsc *.ts. You can also specify multiple files by name (tsc file1.ts file2.ts).

tsc lets you specify an output file name with --out:

tsc --out output/outputfile.js *.ts

TypeScript software packages will typically not require you to run tsc yourself. TypeScript supports a configuration file called tsconfig.json that can contain instructions describing how a TypeScript package should be "compiled". This is likely what you will want to use if you download a piece of software written in TypeScript.

A tsconfig.json TypeScript configuration file can be as simple as this:

{
  "compilerOptions": {
    "outDir": "output"
  },
  "include": ["./*"],
  "exclude": ["node_modules"]
}

TypeScript software will typically also have a package.json that describes how the npm package manager should package and install it.

Packages that have both a tsconfig.json file and a package.json are supposed to be compiled and installed by two commands within the source directory:

npm install typescript --save-dev
npm run compile-typescript

The npm install typescript --save-dev command will install a lot, perhaps 400 MiB, of TypeScript and NPM related trash within the source codes folder. That is apparently needed even if you already have tsc and TypeScript installed.

npm run compile-typescript is supposed to compile the TypeScript package. It will probably fail.

You can start a TypeScript application with npm run start if it, by some miracle, was correctly converted to JavaScript. It probably wasn't and this command will probably fail.

Feel free to leave a command below if you can actually get anything written in that toy TypeScript language working, preferably without having to install a ton of trash in the source tree folder.

TypeScript Software[edit]

The "Lazy-Chess-An-Indie-Chess-Puzzle-Game" is a typical piece of TypeScript garbage. It is licensed under the GNU GPL v3. There's no packages, no install instructions or anything else, just a lot of TypeScript trash. Trying to figure out how to get it working turned out to be a complete and utter waste of time.


Add your comment
LinuxReviews welcomes all comments. If you do not want to be anonymous, register or log in. It is free.