Notes on Typescript

How to Use TypeScript – Beginner-Friendly TS Tutorial, https://www.freecodecamp.org/news/an-introduction-to-typescript/ , March 2022

Based on above article the procedure to create a simple console-based typescript project is:
npm i typescript --save-dev
npx tsc --init
---
I tried out the above and it worked as expected.

Now .ts files can be coded in VSCode.
To transpile .ts to .js, the article states that one can use: tsc <file-name.ts> 
That creates <file-name>.js

When I tried out 'tsc <file-name.ts>', I got an error that tsc is not recognized. The following worked:
'.\node_modules\.bin\tsc .\test.ts' 
It created test.js.

'npx tsc .\test.ts' also worked. ... https://www.typescriptlang.org/download/ says one should use 'npx tsc'. Hmm.
https://docs.npmjs.com/cli/v7/commands/npx states: "Run a command from a local or remote npm package" .. "If any requested packages are not present in the local project dependencies, then they are installed to a folder in the npm cache, which is added to the PATH environment variable in the executed process. " So that seems to mean that npx does not redownload a package if the package is present in local project dependencies.
A short post agreeing with above: Use NPX To Run Local And Remote Node Packages, https://aregsar.com/blog/2020/use-npx-to-run-local-and-remote-node-packages/ , Apr. 2020.

I don't know how 'npx tsc <filename>' command figures out that the associated package is 'typescript'.

Another option is to install it globally - 'npm install typescript -g', Ref: https://stackoverflow.com/questions/39404922/tsc-command-not-found-in-compiling-typescript. But I am not sure of the downside of doing that.

'node test.js' ran the 'transpiled' JavaScript program and produced the expected output.

Normally typescript gets included in the project by default (but which can be changed) as part of 'npx create-next-app' and similar commands which create boilerplate project code. The above info. helps in setting up typescript for simple projects like a console test program.

Comments