React: The Ultimate Guide
React: The Ultimate Guide
HTML
TO
REACT
The Ultimate Guide
NGNINJA
ACADEMY
NgNinja Academy | All Rights Reserved
TypeScript
Table Of Content
1 / 34
NgNinja Academy | All Rights Reserved
Partial Type
Required Type
Readonly
Pick
Omit
Extract
Exclude
Record
NonNullable
Type guards
typeof
instanceof
in
2 / 34
NgNinja Academy | All Rights Reserved
What is TypeScript?
More on TypeScript
3 / 34 ?
NgNinja Academy | All Rights Reserved
Generics
Type annotations
Above 2 features are not available in ES6
Basically, TypeScript acts as your buddy programmer when you are pair programming with
someone
It supports OOP
Automated documentation
4 / 34
NgNinja Academy | All Rights Reserved
No boilerplate code
5 / 34
NgNinja Academy | All Rights Reserved
Install TypeScript
Open terminal
Run following command to install typescript globally
You can access it from any folder
You will need yarn or npm installed already on your system
TIP: You can play around with TypeScript using their o cial playground
6 / 34
NgNinja Academy | All Rights Reserved
tsc --init
{
"compilerOptions": {
"target": "es5",
"noImplicitAny": true,
"outDir": "public/js"
"rootDir": "src",
},
}
Run the following command from your project root to compile all the typescript les into
JavaScript
It will throw an error if any part of the le is not following the rule-set you de ned under
compilerOptions
7 / 34
NgNinja Academy | All Rights Reserved
tsc
You can also compile single le by running the command like below
tsc ninjaProgram.ts
tsc -w
Data types
any
Supertype of all data types
Its the dynamic type
Use it when you want to opt out of type checking
Builtin types
Number, string, boolean, void, null, unde ned
Number is double precision -> 64-bit oats
User de ned types
Arrays, enums, classes, interfaces
Examples
8 / 34
NgNinja Academy | All Rights Reserved
More Examples
9 / 34
NgNinja Academy | All Rights Reserved
Variable scopes
Global scope
Declare outside the programming constructs
Accessed from anywhere within your code
function printName() {
console.log(name)
}
Class scope
Also called elds
Accessed using object of class
Static elds are also available... accessed using class name
class User {
name = "sleepless yogi"
}
console.log(yogi.name)
10 / 34
NgNinja Academy | All Rights Reserved
Local scope
Declared within methods, loops, etc ..
Accessible only withing the construct where they are declared
vars are function scoped
let are block scoped
Class inheritance
class PrinterClass {
doPrint():void {
console.log("doPrint() from Parent called…")
}
}
// outputs
doPrint() from Parent called…
11 / 34
NgNinja Academy | All Rights Reserved
Data hiding
Access modi ers and access speci ers are used for this purpose
Public
class User {
public name: string
console.log(yogi.name) // valid
console.log(yogi.getName()) // valid
Private
Member are accessible only within its class
class User {
12 / 34
NgNinja Academy | All Rights Reserved
// private member
#name: string
constructor(theName: string) {
this.name = theName;
}
}
console.log(yogi.name) // invalid
Protected
Similar to private members
But, members are accessible by members within same class and its child classes
class User {
// protected member
protected name: string
constructor(theName: string) {
this.name = theName;
}
}
public getDetails() {
return `Hello, my name is ${this.name} and I study in
${this.college}.`;
}
}
console.log(yogi.name) // invalid
console.log(yogi.college) // invalid
console.log(yogi.getDetails()) // valid
13 / 34
NgNinja Academy | All Rights Reserved
Interface
Example
IPerson is an interface
It de nes two members
name as string
sayHi as a function
interface IPerson {
name: string,
sayHi?: () => string
}
14 / 34
NgNinja Academy | All Rights Reserved
Optional members:
So, the objects that implements that interface need not define the "sayHi"
function
Namespaces
namespace SomeNameSpaceName {
export interface ISomeInterfaceName { }
15 / 34
NgNinja Academy | All Rights Reserved
Classes or interfaces which should be accessed outside the namespace should be marked with
keyword export
//FileName : MyNameSpace.ts
namespace MyNameSpace {
// nested namespace
export namespace MyNestedNameSpace {
}
}
}
16 / 34
NgNinja Academy | All Rights Reserved
Enums
enum Direction {
Up = 1,
Down,
Left,
Right
}
let a = Enum.A;
let nameOfA = Enum[Enum.Up]; // "Up"
17 / 34
NgNinja Academy | All Rights Reserved
any vs unknown
void vs never
void return void, never never return
void can be thought of as a type containing a single value
no means to consume this value though
but a void function can be thought of returning such value
never is a type containing no values
meaning function with this return type can never return normally at all
either throw exception or reject promise or failing to terminate
If you know the types you can just start using things from other modules
Without worrying about breaking your code
doSomething(m) {
// m.count is undefined
// and undefined not greater than 2
// so returns "small"
return m.count > 2 ? 'big' : 'small'
19 / 34
NgNinja Academy | All Rights Reserved
validate(arr) {
if(!Array.isArray(arr)) {
throw new Error('arr must be an Array')
}
}
Type assertion
S is a subtype of T
OR...
T is a subtype of S
Example
20 / 34
NgNinja Academy | All Rights Reserved
21 / 34
NgNinja Academy | All Rights Reserved
Generics
Gives ability to create a component that can work over a variety of types
Rather than only a single one
Consumer can then consume these components and use their own types
Simple example
Basically we can use Generics to create one function to support multiple types
22 / 34
NgNinja Academy | All Rights Reserved
Advanced example
// T will number
// it will return 3
const lastNum = last([1, 2, 3]);
// T will be string
// it will return "c"
const lastString = last(["a", "b", "c"]);
Intersection Types
23 / 34
NgNinja Academy | All Rights Reserved
type A = {
id: number
foo: string
}
type B = {
id: number
bar: string
}
type A = {
bar: number
}
type B = {
bar: string
}
Union Types
24 / 34
NgNinja Academy | All Rights Reserved
val = 12
console.log("numeric value of val " + val)
Advanced example
You can combine intersection types and union types to x the above code snipped
Like below
type A = {
bar: string | number
}
type B = {
bar: string | number
}
25 / 34
NgNinja Academy | All Rights Reserved
type C = A & B
Partial Type
It is a utility type
Can be used to manipulate types easily
Partial allows you to make all properties of the type T optional
Partial<T>
Example
type Customer {
id: string
name: string
age: number
}
26 / 34
NgNinja Academy | All Rights Reserved
Required Type
type Customer {
id: string
name?: string
age?: number
}
Readonly
type Customer {
id: string
name: string
}
27 / 34
NgNinja Academy | All Rights Reserved
type Customer {
id: string
readonly name: string
}
Pick
type Customer {
id: string
name: string
age: number
}
28 / 34
NgNinja Academy | All Rights Reserved
// logic
}
Omit
type Customer {
id: string
name: string
age: number
}
29 / 34
NgNinja Academy | All Rights Reserved
Extract
type Customer {
id: string
name: string
age: number
}
type Employee {
id: string
name: string
salary: number
}
Exclude
type Customer {
id: string
name: string
age: number
}
30 / 34
NgNinja Academy | All Rights Reserved
type Employee {
id: string
name: string
salary: number
}
Record
type Customer {
id: string
name: string
age: number
}
NonNullable
31 / 34
NgNinja Academy | All Rights Reserved
Then if you pass null or undefined to the type your app linter will throw an error
addTask("task-1")
// valid call
addTask(1)
// valid call
addTask(null)
// Error: Argument of type 'null' is not assignable
addTask(undefined)
// Error: Argument of type 'undefined' is not assignable
Type guards
typeof
32 / 34
NgNinja Academy | All Rights Reserved
else {
// No! not a number logic
}
instanceof
class Task {
// class members
}
class Person {
// class members
}
in
type Task {
taskId: string
description: string
33 / 34
NgNinja Academy | All Rights Reserved
34 / 34