October 2017
Intermediate to advanced
280 pages
6h 50m
English
Just like Java and C++, TypeScript allows us to explicitly declare the type of the given variable:
let foo: number = 42;
The preceding line defines the foo variable in the current block using the let syntax. We explicitly declare that we want foo to be of the number type and we set the value of foo to 42.
Now, let's try to change the value of foo:
let foo: number = 42; foo = '42';
Here, after the declaration of foo, we will set its value to the '42' string. This is a perfectly valid JavaScript code; however, if we compile it using the TypeScript's compiler, we will get this:
$ tsc basic.ts basic.ts(2,1): error TS2322: Type 'string' is not assignable to type 'number'.
Once foo is associated with the given ...
Read now
Unlock full access