-
Book Overview & Buying
-
Table Of Contents
Full-Stack Web Development with TypeScript 5
By :
Let’s start with two main aspects of TypeScript – simple types and interfaces.
JavaScript has types, but they are implicit and not strictly enforced. In TypeScript, you’ll find most types are explicit, and you’re required to declare them. Let’s declare one:
let messageText: string = "my first chat message"
The difference you can see with the ordinary JavaScript is : string. This part defines the time of the variable that we are going to use. Here, we’ve clearly stated that messageText is a string. If we try to assign a value of a different type, like a number, we’ll get an error:
messageText = 5
This line will trigger an error since 5 is not a string.
Another benefit is when the compiler knows the variable is a string, your IDE will suggest actual functions that exist on the string type, which is quite useful. Similarly, we can use other basic types to annotate variables...