March 13, 2024
Posted By: Nandan Sinha
Tag:
Technology & Innovation
TypeScript adds a type-checking step to JavaScript development. On AlmaLinux, a useful starting point is a working Node.js and npm installation, followed by a project-local TypeScript compiler. Keeping the compiler with the project makes its version easier for a team to reproduce.
Check the prerequisites
cat /etc/os-release
node --version
npm --version
If Node.js or npm is missing, install a supported version using an approved method for your AlmaLinux release. Repository streams and package versions vary, so verify what the selected source provides rather than assuming every installation is identical.
You do not need to run a system-wide upgrade merely to create a TypeScript project. Handle server maintenance through your normal update process.
Create a project and install the compiler
Run these commands as your development user in a new directory. If you are working in an existing repository, follow its package manager and lockfile conventions instead.
mkdir typescript-demo
cd typescript-demo
npm init -y
npm install --save-dev typescript
npx tsc --version
The dependency is recorded in package.json. Review and retain the lockfile according to the project’s workflow. A global installation is also possible, but it is not required for this example.
Compile a small example
Create main.ts with the following content:
const message: string = "Hello from TypeScript";
console.log(message);
Compile the file, then run the generated JavaScript:
npx tsc main.ts
node main.js
This explicit-file example does not rely on a tsconfig.json. When you are ready for project configuration, generate one with npx tsc --init, review its options, then run npx tsc without a file argument to compile according to that configuration.
Understand the boundary of type checking
Type checking can catch certain mistakes before execution. It does not automatically validate an API response or make untrusted input safe at runtime. A value entering the application still needs the checks appropriate to its source and purpose.
Maintain or remove the dependency
Test compiler upgrades in the project before merging them. To remove the local compiler dependency, use:
npm uninstall --save-dev typescript
Check the resulting changes to package.json and the lockfile. Repeating the uninstall command is not a verification step; reviewing the dependency changes is.
Official reference
TypeScript installation and project setup
Leave a Reply