-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathTypeScript.txt
97 lines (59 loc) · 3.13 KB
/
TypeScript.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
TypeScript:
To install TS, you need to install npm first(nodejs)
check version of npm by "npm -v"
install command: npm install -g typescript
check the version: "tsc -v"
---->every browser will have one engine to run/interpret js
eg:google chrome-->v8,firefox--->spiderMonkey
-->any browser cannot understand ts. so u need a compiler which converts ts into js.The compiler is typescript compiler , also called as "tsc"
In VS code, always try to prefer command prompt
compile command: 1. npx tsc --init , it will create tsconfig.json which sets rules while converting ts into js
2. npx tsc filename/. which will create js file
Typescript data types:
Typescript follows two methods for data type declarations
1. Type Annotations: it means we declare data type followed by : , after the variable
2.Type Interference: it means typescript can guess the data type based on initial value
Data types:
number,string,boolean,undefined,null,void,tuple,any,
enum: enum is a data type given to constant value(a set of constants)
never: never is data type which defines the situation/function that never should run
union( | )==> used to define multiple data types
any means it is allowed to take any data type
Task1: create a variable "name" which accepts number or string and create a variable value which accepts any data type and print their types in console
let s:number|string="raju";
let w:any=321;
console.log(typeof s)
console.log(typeof w)
Task2: create an array which accepts strings and numbers only
Tuple: tuple is used when we need to declare data types for individual array elements;
let arr:[number|string,any]=["",true];
Function: every function need to define it parameters and return type
void is used to defined a function return type when the function is not returning anything
In function parameters you can give optional parameters by using '?' and you can even define default value using parameter=value
We represents objects in two ways
1. Interfaces
interface is user defined data type specifically for objects
interface myObj{
name:string,
age:number
}
2.type alias
type x=number|string|boolean;
let a:x="ghj"
//access modifiers
it means how access will be given to the following elements
Public - it can accessed by any class,Private - it cannot be accessed by any class except its own,Protected - it can accessed itself and its children
readonly,static keywords
modules-- ES6
import and export are keywords in ES6
Type Assertion: sometimes type interferance will not work correct , so at that times we define value as our known data type
Yesterday we discussed,never,enum,interfaces,type alias,class implementation,access modifiers,access specifiers,type Assertion
Advanced Topics:
Generics: Generics are used to define general dat type which can be named while invoking class/function
function fun<P>(a:P):P{}
fun<number>(2)
Declaration Files:
if i'm using any third party libraries, we use declaration files to define data types for 3rd party code
it's extension is .d.ts
namespaces:namespaces are used to group your functionalities. it works while exporting from one file to another file.
Decorators: always we use @ before a decorator