TypeScript Cheat Sheet
Last updated
Last updated
Command-line interface for Angular - set of commands that will help us during development.
This is ES functionality
Spread
Spread can be used to merge two object or arrays:
both variable is equal to [0, 1, 2, 3, 4, 5].
Same for objects:
Second is equal to {name: "test", id: 2 }
Destructuring
Reversed spread - we can split one object into multiple
first is equal to 1 and second is equal to 2
We can also use "..." operator
first is equal to 1
rest is equal to [2, 3]
This is ES functionality
This is ES functionality
Sample class:
Notes:
Only one constructor inside a class
constructor(this message: string) will automatically create message property and assign to its value from message property
Instead of private - public or protected can be used
TypeScript supports getters/setters as a way of intercepting accesses to a member of an object. You can add logic while the property is changing value or it is read by something
Abstract classes are base classes from which other classes may be derived. They may not be instantiated directly. Unlike an interface, an abstract class may contain implementation details for its members.
getValue function must be implemented in derived classes. A derived class must also call super() in a constructor, example:
A class or struct that implements the interface must implement the members of the interface that are specified in the interface definition. The interface is the only skeleton for our classes implementing them
Interface for object
obj parameter has to contain label property
Interface for class
class MadeFromString has to contain 'name' property
obj parameter has to contain label property
Index signature can be used to allow object/class contain multiple different properties with the same type, for example, we have such an interface:
and MyObject that is implementing it.
Sample correct implementation of IMyObject:
Sample incorrect implementation of IMyObject:
Because age is number and our index signature indicate that our properties should have a string type
Sometimes same function/class/object can be used with different types and we don't want to declare it to one specific type. We can use generic type then
Example:
and now it can be used with diffrent types used for value parameter:
More than just one generic type can be used:
Generic classes can be extended - it can be the implementation of some interface/object. Extending allow your object to inherit the features of another object type, so can be used for instance when you expect that object will contain some of properties or functions that will be used further.
Example:
arg has to object that is equal or extending string, for example, string or:
another example - let's assume that we need to use length property from our object, so we would like to be sure that this property will exist in object. Let's create ILengthwise interface:
and now we can use it to ensure that our generic will contain length property:
Enums allow us to define a set of named constants and then used them like a dictionary.
Sample enum:
usage:
Some enum members have pre-set value and some members are computed during compilation
member variable is equal to "A"
Intersection
Type1 & Type2 means that there will be object containing properties both from Type1 & Type2
Sample correct myObject value:
Sample incorrect myObject values:
Union
Value can be string OR boolean.
It is correct:
and that is incorrect:
Type guard can be used to tell TypeScript what type is any value in a specific block of code.
Sample Type Guard implementation:
Sample Type Guard usage
thanks to 'x is number' typescript know in which scope we have access to toPrecision
Own custom types can be declared, for example
Usage:
Types
Create a tree structure for an object. You can't do the same with the interface because of lack of intersection (&)
type to restrict a variable to assign only a few values. Interfaces don't have union (|)
thanks to types, you can declare NonNullable type thanks to a conditional mechanism.
Interfaces You can use interface for OOP and use 'implements' to define object/class skeleton
You can extend interfaces with other interfaces
Generic types can be different depending on some condition
if T extends string (contains all string properties and functions) then it will be a string type. Otherwise, it will be our custom Text type.
another example:
NonNullable type will check if a avalue isn't equal to null or undefined (it should never be equal to null or undefined)
keyof keyword is indicating all keys for specifed object, for example:
only "id" or "age" values can be assigned to variable with 'testType' type
Example 2:
using [keyof ...] instead of "keyof" before object means that we are revesing keyof behavior and now only 1 and "T" can be assigned to value with Keys type (values, not property keys)
Example 3:
This will point to all keys in the T object (just like keyof T). But this construction can be used to for example check type of keys:
This type will require values equal to keys of T object, but only these ones which are functions
"Within the extends clause of a conditional type, it is now possible to have infer declarations that introduce a type variable to be inferred. Such inferred type variables may be referenced in the true branch of the conditional type. It is possible to have multiple infer locations for the same type variable."
Example:
My value can be type number | string | boolean (number or string or boolean)
Instead of using normal string keys in object it can be used predefined and assigned to variable Symbol example:
Symbols are unique
sym2 is not equal to sym3 (sym2 != sym3)
Import and export mechanism (between files and objects)
Example:
data.ts file
Import other file
Import all:
Encapsulated scope of classes, objects, functions, interfaces and others.
Example:
Encapsulated scope of classes, objects, functions, interfaces, and others - that is available to export/import. "We call declarations that don’t define an implementation “ambient”. Typically, these are defined in .d.ts files. "
Example
and then we can import it
Some functions in JS are global and we may have not type for them. We can use 'declare' keyword here
"Decorators provide a way to add both annotations and a meta-programming syntax for class declarations and members."
it will change return value of decorated function so it will return "Hello " + value from decorator
greet() will return "Hello, here is hacker"
Loops can be executed "step by step"
Example:
Q: What is Tuple type?
A: Array contains items with various types. See types
Q: When never type can be useful?
A: Never is information that this particular part shouldn't be reachable. For example in this code
we have an infinite loop and we don't want to iterate infinite loop. Simply as that.
But a real question is how can it be useful for us? It might be helpful for instance while creating more advanced types to point what they are not
For example, let's declare our own NonNullable type:
Here we are checking if T is null or undefined. If it is then we are pointing that it should never happen. Then while using this type:
Q: What is a difference between named, anonymous and arrow functions?
A: See basic function types
Q: How many constructors can be implemented for class
A: Only one - there is no overloading for TypeScript class constructor
Q: What is a difference between "extends" and "implements"
A: Classes can extend other classes or objects, but when it comes to interfaces then they should be implemented
For MyObject
For IMyObject
Q: What is a difference between types and interfaces?
A: See Difference between types and interfaces
Q: What is keyof?
A: keyof keyword is indicating all keys for specifed object, for example:
testType = "id" | "age"
See Index types
Q: What is tsconfig.json file?
A: File specifies the root files and the compiler options required to compile the project.
Q: What is computed enum value?
A: Computed value will be assigned during compilation. See Enum computed value
Q: What are files with the d.ts extension for?
A: They are usually module template file where we can keep typings for JS code.
Q: List few predefined conditional types in TypeScript
A:
Exclude<T, U> – Exclude from T those types that are assignable to U.
Extract<T, U> – Extract from T those types that are assignable to U.
NonNullable – Exclude null and undefined from T.
ReturnType – Obtain the return type of a function type.
InstanceType – Obtain the instance type of a constructor function type.
See TypeScript 2.8
Q: What will be type box here?
A: {name: string, age: number};
Q: What is 'unknown' type in TypeScript?
A: Unknown is used to describe the least-capable type in TypeScript. It doesn't allow typical operations for a particular type
Q: Can typescript be used without JavaScript?
A: No, TypeScript is compiled to JavaScript, so it can't be used without it
Q: What is DefinitelyTyped repository?
A: It's GitHub repository with already written types for most popular JS libraries. They can be installed by npm with @types prefix, for example npm install @types/node
Type
Example
Notes
number
let myNumber: number = 6
Represent all numbers : integer, float, hex etc.
string
let fullName: string = Bob Bobbington
Text with all text functions (indexOf, replace etc.)
boolean
let isTrue: boolean = true
true/false value
Array
let list: Array = [1, 2, 3]
Shorter version: let list: number[] = [1, 2, 3];
Tuple
let x: [string, number] = ['test', 2];
Array contains ains items with various types
any
let x: any = 2;
Everything can be assigned to value with any type
Enum
enum Color {Red, Blue }
Usage: let c: Color = Color.Blue;
Never
function fail(): never {while(true) {}}
Indicating that something never should happen
Null or undefined
let u: undefined = undefined;
In TS there is dedicated type for null and undefined
Object
let x:Object = {id: 2};
Represents any object
Function
let myFn:Function = function() {...}
Represents any function
Function
Result
Notes
concat
numArray.concat(numArray2) -> [1,2,3,4,5]
Push values from one array to another and return it as a new array
every
objectArray.every(x=> x.age > 20) -> true
Check if all items are passing condition
some
objectArray.some(x=> x.age > 30) -> true
Check if any item is passing condition
filter
objectArray.filter(x=> x.age >= 35)) -> [{name: "Iva", age: 39}]
Get only items which are passing condition
forEach
numArray.forEach(x=> console.log(x))
Perform some logic for each array item
join
stringArray.join(",") -> "a,b,c"
Merge text items into one value (add ',' between)
indexOf
stringArray.indexOf("b") -> 1
Return index(position) array item
map
stringArray.map(x => x + "1") -> ["a1", "b1", "c1"]
Create new array/object applying some logic for each element
push
stringArray.push("d") -> ["a", "b", "c", "d"]
Add element to the end of the array
unshift
stringArray.unshift("d") -> ["d", "a", "b", "c"]
Add element to the begin of the array
reduce
numArray.reduce((x, y) => x + y) -> 16
Merge array into one value performing some logic on a way
shift
numArray.shift() -> [2, 3]
Removes the first element from an array and returns it.
sort
["b", "c", "a"].sort((x,y) => x > y) -> ["a", "b", "c"]
Sort array by some logic
Type
Example
Named function
function add(x: number, y:number): number {}
Anonymous function
let myAdd = function(x: number, y: number = 1): number
Arrow function
myArrowAdd = (x: number, y:number): number