In this blog post, we will see how can we work with the variables in Typescript.
This is part of the Typescript Tutorial Series, whose index / TOC is available here → Typescript Tutorial Series.
We use the
let keyword to define a variable in Typescript. As indicated the value of the variable is NOT fixed (hence the name) and can be changed at any point in time.
Advantage of type checking
If such kind of data type mismatch errros are not caught earlier (during the compilation time), they may blow up during the execution of the source code in the runtime, which is certainly recommended as it will disappoint the actual users of the application.
The syntax to define a variable goes as follows.
let <varName> : <dataType> = <initialValue>;
letis a keyword
varNameis the name of the variable
dataTypeis one of the valid types that can be assigned to a variable in
initialValueis the potentially allowed values according to the
;is a line terminator
The Typescritp datatypes are discussed in this post → Typescript Datatypes
Example 1 - Proper data assignment
let age : number = 21; console.log(age);
The above program is legally valid as it follows the norms, by assiging a numeric data to the variable
age whose type is defined as
Example 2 - Invalid data assignment
let age : number = 'A'; console.log(age);
First of you all, the IDE (VSCode, Eclipse, or any others which has got the
tsc embedded) will show a warning in the source code with an icon or bulb indicating that error.
If you still go ahead and compile (transpile) the program, the
tsc will throw an error, as follows.
typescriptPractices > tsc variables.ts variables.ts:1:5 - error TS2322: Type 'string' is not assignable to type 'number'. 1 let age : number = 'A'; ~~~ Found 1 error. typescriptPractices >
The error message is pretty clear and self-explanatory.
Note: There is a caveat in this step - as the
tscwill still produce the
.tsfile despite the compiler errors. We discuss how to avoid this in the post.
Example Program with a few different type of variables
The below program demonstrates the usage of the primitive type variables with the valid values.
let found : boolean = true; let mark : number = 99.1; let firstName : string = 'Raghavan'; //surrounded by a pair of single quotes let lastName : string = "Muthu"; //surrounded by a pair of double quotes console.log(found); console.log(mark); console.log(firstName); console.log(lastName);
Let us verify the file status.
typescriptPractices > ls -l total 4 -rw-rw-rw- 1 raghs 0 222 2021-06-19 21:48 variables.ts typescriptPractices >
We transpile the program using
ls command confirms the availability of
.js file as a result of transpilation by the Typescript Compiler
typescriptPractices > tsc variables.ts typescriptPractices > ls -l total 8 -rw-rw-rw- 1 raghs 0 183 2021-06-19 21:49 variables.js -rw-rw-rw- 1 raghs 0 222 2021-06-19 21:48 variables.ts
Then, execute the program using the node runtime -
node with the generated/transpiled
typescriptPractices > node variables.js true 99.1 Raghavan Muthu
Raghavan alias Saravanan Muthu
19 Jun 2021 | Sat | 21 50 25 PM IST