One is by defining interfaces and the other is defining a type alias. In addition to defining data types for primitive values, we can define data type annotations for objects. There are two ways to define types for objects. This lets us avoid a lot of mistakes with data types when programming. If we call add with anything other than two numbers, we will get a type error from the TypeScript compiler. It takes parameters a and b which are both numbers.Īnd we specify that the type of the return value of the function is number after the colon that comes after the function signature. We can add data type annotations to parameters and return values of functions with TypeScript.įor instance, we write: const add = (a : number, b : number ) : number => a + b If we assign something that doesn’t match the type we specify, we’ll get errors from the TypeScript compiler. We declare a variable a which is a number, and variable b which is a string. We can assign numbers to number variables.Īnd we can assign strings to string variables. To declare variables with data type annotations with TypeScript, we write something like: const a : number = 1 const b : string = "foo" But we can add data type annotations to variables with TypeScript. In JavaScript, we can define variables but we can’t add type annotation to them. One of the basic TypeScript features that we use regularly in our apps is variable declarations. TypeScript comes with some common language features that we use to write programs. In this article, we will look at basic TypeScript language features and how we can use them in our Angular apps. We can use all TypeScript features in our Angular apps since the Angular CLI comes with the TypeScript compiler built in. This makes programming easier since we don’t have to check the data type of variables and other values ourselves. TypeScript mainly adds data type checking capabilities that are missing in JavaScript. TypeScript is a superset of JavaScript and it supports all language features included with JavaScript. It is based on the TypeScript programming language. Let’s look at basic TypeScript language features and how we can use them to enhance our Angular apps.Īngular is a framework that lets us create interactive web frontends for users.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |