Angular / Basics / Variables : set & get
Variables : set & get
-
Note
S.No. Variable Declaration Syntax & Description 1. var name:string = ”mary”
The variable stores a value of type string
2. var name:string;
The variable is a string variable. The variable’s value is set to undefined by default
3. var name = ”mary”
The variable’s type is inferred from the data type of the value. Here, the variable is of the type string
4. var name;
The variable’s data type is any. Its value is set to undefined by default.
var name:string = "John"; var score1:number = 50; var score2:number = 42.50 var sum = score1 + score2 console.log("name"+name) console.log("first score: "+score1) console.log("second score: "+score2) console.log("sum of the scores: "+sum) 1. Arrays
Declaring and Initializing Arraysvar array_name[:datatype]; //declaration array_name = [val1,val2,valn..] //initialization Examples
var alphas:string[]; alphas = ["1","2","3","4"] console.log(alphas[0]); console.log(alphas[1]); var nums:number[] = [1,2,3,3] console.log(nums[0]); console.log(nums[1]); console.log(nums[2]); console.log(nums[3]); an array is a collection of values of the same data type 2. Tuples
At times, there might be a need to store a collection of values of varied types. Arrays will not serve this purpose. TypeScript gives us a data type called tuple that helps to achieve such a purpose.
Syntax
Examplevar tuple_name = [value1,value2,value3,…value n]
ORvar mytuple = [10,"Hello"]; console.log(mytuple[0]) console.log(mytuple[1]) var mytuple = []; mytuple[0] = 120 mytuple[1] = 234 console.log(mytuple[0]) console.log(mytuple[1]) 3. Object Type
using javascript
using typescriptconst person = { firstName: "John", lastName: "Doe", age: 50, eyeColor: "blue" }; const car: { type: string, model: string, year: number } = { type: "Toyota", model: "Corolla", year: 2009 }; car.type = "Ford"; console.log(car.type); 4. Enum type
An enum is a special "class" that represents a group of constants (unchangeable variables). enum StatusCodes { NotFound = 404, Success = 200, Accepted = 202, BadRequest = 400 } console.log(StatusCodes.NotFound); 5. Type Aliases
Type Aliases allow defining types with a custom name (an Alias). Type aliases don’t define new types; instead, they provide an alternative name for an existing type. type CarYear = number type CarType = string type CarModel = string type Car = { year: CarYear, type: CarType, model: CarModel } const carYear: CarYear = 2001 const carType: CarType = "Toyota" const carModel: CarModel = "Corolla" const car: Car = { year: carYear, type: carType, model: carModel }; 6. interfaces
Interfaces are similar to type aliases, except they only apply to object types.
Extending Interfacesinterface Rectangle { height: number, width: number } const rectangle: Rectangle = { height: 20, width: 10 };
Extendsinterface Rectangle { height: number, width: number } interface ColoredRectangle extends Rectangle { color: string } const coloredRectangle: ColoredRectangle = { height: 20, width: 10, color: "red" }; 1. A type alias can extend another type alias using an ampersand:
type A = { x: number }; type B = A & { y: string }; 2. Type aliases can also extend interfaces:
interface A { x: number; } type B = A & { y: string }; 3. Interfaces can extend type aliases with the extends keyword:
type A = { x: number; }; interface B extends A { y: string; } 4. Interfaces can also extend multiple interfaces separated by commas.
interface A { x: string; y: number; } interface B { z: boolean; } interface C extends A, B { breed: string; } let c: C = { x: "Sparky", y: 4, z: true, }; 1. Declaration merging only works on interfaces. TypeScript will give an error if you declare the same type alias more than once. Declaration merging
2. You cannot declare a union, intersection or tuple with the interface keyword.interface Person { name: string; } interface Person { age: number; } type Num = numbers; type Num = string; // Duplicate identifier Num let user: Person = { // has the properties of both instances of Person name: "Tolu", age: 0, }; interface Person is declared two times Unions and Intersections//Intersection type User = { readonly id : string, name: string, email: string, } type Admin = User & { key: string, } // You can use Admin like this: let newAdmin: Admin = { id: "3KD5D874", name: "Lucifer", email: "lucifer@hell.com", key: "Oh my me!", }; //Union type User = { name: string, id : number } type Admin ={ username: string, id: number, key: number, } let newUser : User | Admin; // ✅ CORRECT newUser = {name: "John", id: 123}; newUser = {username : "j333", id : 123, key: 345}; newUser = {name: "j333", id : 123, key: 345}; // ❌ ERROR: Property 'key' is missing newUser = {username : "j333", id : 123};