Typescript
Sat Sep 17 2022 11:23:29 GMT+0000 (Coordinated Universal Time)
Saved by @SamMrch #typescript #angular
typescript adds types to JavaScript.
/////////////////////////////////////////// function arguments
Function arguments & return types:
let greeting : (name: string) => string;
greeting = function (name: string) {
return `Hi ${name}`;
};
/////////////////////////////////////////// object Type
let employee: object;
object type represents any value that is not a primitive type,while Object type describes functionalities that available on all objects;toString,valueof...
//////////////////////////////////// array & tuple
array can store mixed type values
let arr: type[]
A tupple is an array with a fixed number of elements whose types are known.
//////////////////////////////////// any type
Use the any type to store a value that you don’t actually know its type at the compile-time
//////////////////////////////////// void type
you use the void type as the return type of functions that do not return a value.
//////////////////////////////////// union types
combine multiple types into one type
let result: number | string;
/////////////////////////////////////////// type aliases
define a new name for an existing type,useful for union types.
type newName=existing type;
type alphanumeric = string | number;
/////////////////////////////////////////////// String Literal Types
define a type that accepts only a specified string literal;
let mouseEvent: 'click' | 'dblclick' | 'mouseup' | 'mousedown';
///////////////////////////////////// Type inference
TypeScript guesses the type,when you initialize the variable.
///////////////////////////////////// optional parameters
Use the parameter?: type syntax to make a parameter optional.
Use the expression typeof(parameter) !== 'undefined' to check if the parameter has been initialized.
///////////////////////////////////// default parameters
function name(parameter1=defaultValue1,...) {
// do something
}
To use the default initialized value of a parameter, you omit the argument when calling the function or pass the undefined into the function.
///////////////////////////////////// classes
ES5 allows classes and Ts adds type annotations
///////////////////////////////////// inheritance
you use the extends keyword.
To call the constructor of the parent class in the constructor of the child class, you use the super() syntax.
Method overriding:just redefine the method
/////////////////////////////Static Methods and Properties
a static property is shared among all instances of a class.
///////////////////////////// abstract classe
define common behaviors for derived classes to extend,
///////////////////////////// type guards
used to identify or narrow down the type of a variable using instanceOf and typeOf.
User can define type-guards:
function isCustomer(partner: any): partner is Customer {
return partner instanceof Customer;
}
///////////////////// casting types
to convert a variable from one type to another.using as pr <>;



Comments