TypeScript is an open source programming language developed by Microsoft .
It is a strict syntactical superset of JavaScript and adds optional static typing to the language.
This tutorial is especially aimed at programmers who already know JavaScript, it will cover the fundamental concepts of TypeScript and will highlight some of the most important “gotchas”.
TypeScript was developed by Anders Hejlsberg, architect of the [C#] language (https://terminalroot.com/tags#csharp) and creator of the [Delphi] languages (https://www.embarcadero.com/products/delphi ) and Turbo Pascal.
TypeScript supports definition files that can contain type information from existing JavaScript libraries, just as C++ header files can describe the structure of existing object files.
The official address of TypeScript is: https://www.typescriptlang.org/.
You ask:
Even though the official TypeScript documentation itself, the answer is NO!
There are some differences, for example, if you create variables or constants (var
, let
or const
) with the name: name
in TypeScript, it will not work, it will give an error:
You will get the error:
This is because the TypeScript transpiler already uses a declaration with the same name: name
. If you are going to use something to name an object or anything else, use examples like this:
But, in JavaScript this is no problem, examples:
This code below also DOES NOT WORK IN TypeScript!
the cool thing about JS is that it has several easter eggs pic .twitter.com/Jbks00bTTX
— Terminal Root (@TerminalRootTV) May 24, 2024
console.log(++[[]][+[]]+[+[]])
However, if you come from other programming languages, you will still have some difficulties understanding TypeScript. This, for example, works in TypeScript:
In JS constants are modifiable, this took a while to sink in pic.twitter.com/NVBd9ctCF3
— Terminal Root (@TerminalRootTV) May 5, 2024
To test TypeScript codes, you can write to TypeScript’s Playground. Or install the tsc
command on your machine using NPM, like this:
Then create a basic code, for example, main.ts
(.ts
extension):
Run the file with the tsc
command:
After running this command it will create a file with the same name, but with the extension .js
: main.js
.
And run using a JavaScript runtime, examples: Bun, Node or QuickJS:
iYou can also create a development environment, for example:
This is more interesting for your LSP: How to Configure the LSP for TypeScript in Neovim.
You can modify the tsconfig.json
file, and leave it like this:
It will always compile the main.ts
file just by running:
And it will create a
main.js
inside thedist
folder.
And run the JavaScript file:
Or all at once:
tsc && bun dist/main.js
For more details about the tsc
command use the help:
TypeScript has types, that’s why its name is: TypeScript! 😃
There is no
float
ordouble
it’s allnumber
! But, there are other types primitives like:bigint
,symbol
,unknown
, …
In my case, I have OpenGL installed and it defined types for TypeScript, so the GLfloat
type is available:
My LSP lists all the types for me!
But, if it doesn’t work for you (you have to have OpenGL installed on the system), set it like this:
There is a special type: any
, which you can use whenever you don’t want a specific value to cause typing errors.
Find out more here.
A tuple type is another type of Array
that knows exactly how many elements it contains and exactly which types it contains at specific positions.
Enums allow a developer to define a set of named constants. Using enums can make it easier to document intent or create a set of distinct cases. TypeScript provides numeric and string-based enumerations.
An interface declaration is another way of naming an object type:
Interface
Class
Note:
name
is inside a class! 😃
|
) and intersection (&
) types for greater flexibility.import
and export
) to organize your code.Note: I declared the variable
name
, but as a function argument!
After compiling and running:
As we are going to use the process.stdout.write()
function we need to install @types/node
, so run:
Now read the properly commented code with an explanation of each block:
Spin and don’t forget to play those numbers! 😃
TypeScript offers a powerful way to write typed JavaScript making your code easier to maintain.
With the above basics, you should be well equipped to start using TypeScript in your projects. Remember to take advantage of TypeScript’s features to create more robust code.
Continue your learning and consult the documentation on the official website.