TypeScript - Type Annotations
TypeScript is a typed language, where we can specify the type of the variables, function parameters and object properties. We can specify the type using :Type after the name of the variable, parameter or property. There can be a space after the colon. TypeScript includes all the primitive types of JavaScript- number, string and boolean.
Type annotations are used to enforce type checking. It is not mandatory in TypeScript to use type annotations. However, type annotations help the compiler in checking types and helps avoid errors dealing with data types. It is also a good way of writing code for easier readability and maintenance by future developers working on your code.
We can still follow the JavaScript way of declaring variables and have the TypeScript compiler infer the data type of the variable.
The following example demonstrates the type annotation of paramters.
Similarly, we can declare an object with inline annotations for each of the properties of the object.
Subscribe to:
Post Comments (Atom)
How to build an Express and Node.js app with Typescript
In this tutorial, you will learn how to set up a Node.js and Express project with Typescript and live auto-reloading. Note that this metho...
-
Below is a simple form with two fields and a button. We will use this setup to illustrate the different ways to disable form controls. He...
-
ngx-image-compress demo Upload and compress Image Upload Image only Click For Demo
-
TypeScript is an open-source object-oriented language developed and maintained by Microsoft, licensed under Apache 2 license. It is a typed ...
No comments:
Post a Comment