September 2018
Intermediate to advanced
302 pages
7h 17m
English
TypeScript brings types to our code. We can explicitly express the requirement that a function accepts only a specific variable type. Let's look at how we could use the example from the previous section with types from TypeScript:
type ObjXType = { x: number}function increase(obj: ObjXType) { obj.x = obj.x + 1; return obj;}var numObj = { x: "5" };increase(numObj);console.log(numObj);
This code will not compile. The static check will exit with an error saying that the code base is corrupted because types do not match.
The following error will be shown:
Argument of type '{ x: string; }' is not assignable to parameter of type 'ObjXType'. Types of property 'x' are incompatible. Type 'string' is not assignable to type ...
Read now
Unlock full access