Typescript Flashcards
TypeScript
TypeScript is a typed superset of JavaScript that compiles to plain JavaScript. TypeScript is pure object oriented with classes, interfaces and statically typed like C# or Java.
TypeScript is a strongly typed, object oriented, compiled language. It was designed by Anders Hejlsberg (designer of C#) at Microsoft.
Declaration Files
When a TypeScript script gets compiled, there is an option to generate a declaration file (with the extension .d.ts) that functions as an interface to the components in the compiled JavaScript.
Compiler flag & Description
- –help
Displays the help manual - –module
Load external modules - –target
Set the target ECMA version - –declaration
Generates an additional .d.ts file - –removeComments
Removes all comments from the output file - –out
Compile multiple files into a single output file - –sourcemap
Generate a sourcemap (.map) files - –module noImplicitAny
Disallows the compiler from inferring the any type - –watch
Watch for file changes and recompile them on the fly
Type Guards
Type guards allow you to get the type of variables. After that, you can perform multiple operations based on the type of the particular variables. This also ensures the type-safety.
is
typeof
The never type
The never type in TypeScript represents the values that can never occur. For example, the return type of a function that throws an error is never.
function funcName(): never{
// it throws an exception or never returns
}
void vs. never
In TypeScript, a variable of void type can store undefined as a value.
On the other hand, never can’t store any value.
let val1: void = undefined;
let val2: never = undefined; // error: Type ‘undefined’ is not assignable to type ‘never’.
Union
Two or more data types are combined using the pipe symbol (|) to denote a Union Type. In other words, a union type is written as a sequence of types separated by vertical bars
let value: number | string | boolean;
There are three types of the literal types in TypeScript.
String literal types
Numeric literal types
Boolean literal types
Symbols
In TypeScript, a symbol is a primitive data type that is unique and immutable. The symbols are introduced in ECMAScript 2015 (also known as ES6).
As we use the number, string, or boolean to create the variables of different data-type, we can use the symbol type to create the Symbol.
Symbols are unique and immutable
null vs. undefined
null:
Has its own type null
Must be handled explicitly in logic to avoid errors
undefined:
Triggers default function parameters
Indicates missing parameters or optional parameters
Type Aliases
In TypeScript, type aliases is a way to define a type. It allows you to give a specific name to the type or define a custom type using the ‘type’ keyword.
type StringOrNumber = string | number;
Aliasing Tuples
The tuple alias is used to define the structure of the fixed-size array, which can contain specific types of values in a particular order.
Optional Parameters
function functionName(para1:type1, para2?:type2): returnType{
// function body
}
Extending Interfaces
One interesting feature of interfaces is the ability to extend them, allowing us to create a combination of interfaces. Extending an interface is also known as interface inheritance.
Use extends keyword to extend a single or multiple interfaces in TypeScript.
Access Modifiers
Public:
The public members are accessible from anywhere within and outside the class that defines the member.
Private Access Modifiers:
A private access modifier restricts the access of ** the class member (property or method) to the class where it is declared.** When you add the private modifier to the property or method, you can access that property or method within the same class only.
Protected Access Modifiers:
The protected access modifier is used to define a protected class member (property or method). A protected data member ** is accessible only within the class that defines it or any class that extends it.**
TypeScript - Readonly Properties
TypeScript provides us with readonly keyword to make a property in class, type or interface as read-only. The readonly properties can be accessed outside the class but their values can’t be modified.
Abstraction Classes
The abstract classes are used to achieve abstraction in TypeScript. The abstract class contains only method declaration but not implementation.
We need to implement all abstract methods of the abstract class into the inherited class.
The abstraction is a way to hide the lower-level code implementation from users and some developers.
Furthermore, it is used to show only the required information about the method rather than showing the whole complex implementation of methods.
Duck Typing
The circumstance where an object’s type is decided by its behavior, like methods and attributes, rather than its class is known as “duck typing”.
Intersection Types
In TypeScript, an intersection type combines multiple types into one. Although intersection and union types in TypeScript are similar, they are used in very different ways.
type intersepted_Type = Type1 & Type2;
Type Assertions
Type assertion is a mechanism in TypeScript that informs the compiler of the variable type
Using as operator
Using <> operator
Using object