Skip to content

Commit 556b1d5

Browse files
added ts util type and decorators
1 parent 280c7f7 commit 556b1d5

File tree

4 files changed

+92
-19
lines changed

4 files changed

+92
-19
lines changed

typescript-demo/datatypes.ts

+42-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Employees } from "./interfaces";
2+
13
let fname: string;
24

35
fname = "test";
@@ -116,8 +118,45 @@ function swap(num1: number, num2: number): [number, number] {
116118

117119
type names = string | string[];
118120

119-
let newName : names = ['a','b']
121+
let newName: names = ["a", "b"];
122+
123+
type taskStatus = "In Progress" | "Open" | "Closed";
124+
125+
let newTask: taskStatus = "Open";
126+
127+
let adam: Readonly<Employees> = {
128+
id: 1,
129+
130+
dob: new Date("11-Mar-1986"),
131+
department: "IT",
132+
name: "test1",
133+
salary: 10000,
134+
data: "",
135+
};
136+
137+
let { email: empemail, dob: empdbo } = adam;
138+
139+
console.log(empemail);
140+
console.log(empdbo);
141+
142+
// adam.salary = 20000;
143+
144+
adam = { ...adam, salary: 20000 };
145+
146+
// let empemail = adam.email;
147+
// let dob = adam.dob;
148+
149+
let ramesh: Partial<Employees> = {
150+
id: 1,
151+
152+
dob: new Date("11-Mar-1986"),
153+
department: "IT",
154+
name: "test1",
155+
// salary: 10000,
156+
data: "",
157+
};
158+
159+
type newEmployee = Pick<Employees, "email" | "dob">;
120160

121-
type taskStatus = 'In Progress' | 'Open' | 'Closed';
161+
type newDepartment = Omit<Employees, 'salary' | 'dob'>;
122162

123-
let newTask : taskStatus = 'Open';

typescript-demo/decorator.ts

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Greeter {
2+
greeting: string;
3+
constructor(message: string) {
4+
this.greeting = message;
5+
}
6+
7+
@enumerable(false)
8+
greet() {
9+
return "Hello, " + this.greeting;
10+
}
11+
}
12+
13+
function enumerable(value: boolean) {
14+
return function (
15+
target: any,
16+
propertyKey: string,
17+
descriptor: PropertyDescriptor
18+
) {
19+
console.log(target);
20+
console.log(propertyKey);
21+
descriptor.enumerable = value;
22+
};
23+
}

typescript-demo/interfaces.ts

+24-13
Original file line numberDiff line numberDiff line change
@@ -5,43 +5,43 @@ export interface Address<T> {
55
city?: string;
66
pin?: number;
77
country?: string;
8-
data : T;
8+
data: T;
99
}
1010

11-
let address: Address<number> = {
11+
let address: Required<Address<number>> = {
1212
id: 1,
1313
addr1: "Pune",
14+
addr2: "test",
1415
city: "Pune",
1516
country: "India",
1617
pin: 400000,
17-
data : 10
18+
data: 10,
1819
};
1920

20-
interface Employees extends Address<string> {
21+
export interface Employees extends Address<string> {
2122
id: number;
2223
name: string;
2324
department: string;
2425
salary: number;
2526
email: string;
2627
dob: Date;
28+
addressId: Pick<Address<string>, "id" | "addr1">;
2729
}
2830

29-
3031
interface Department {
31-
id: number;
32-
name: string;
32+
id: number;
33+
name: string;
3334
}
3435

35-
36-
let empList: Employees[] = [
36+
let empList: Readonly<Employees[]> = [
3737
{
3838
id: 1,
3939
4040
dob: new Date("11-Mar-1986"),
4141
department: "IT",
4242
name: "test1",
4343
salary: 10000,
44-
data: '',
44+
data: "",
4545
},
4646
{
4747
id: 2,
@@ -50,7 +50,7 @@ let empList: Employees[] = [
5050
department: "IT",
5151
name: "test2",
5252
salary: 15000,
53-
data : '',
53+
data: "",
5454
},
5555
{
5656
id: 3,
@@ -59,10 +59,21 @@ let empList: Employees[] = [
5959
department: "IT",
6060
name: "test3",
6161
salary: 20000,
62-
data : ''
62+
data: "",
6363
},
6464
];
6565

66+
let newEmp = {
67+
id: 4,
68+
69+
dob: new Date("11-Mar-1990"),
70+
department: "IT",
71+
name: "test3",
72+
salary: 20000,
73+
data: "",
74+
};
75+
empList = [...empList, newEmp];
76+
6677
let findEmp = empList.filter((emp) => emp.salary > 10000);
6778

6879
console.table(findEmp);
@@ -75,7 +86,7 @@ export interface AddressUtil {
7586
add(num1: number, num2: number): number;
7687
}
7788

78-
let testUnion : Employees[] | Department;
89+
let testUnion: Employees[] | Department;
7990

8091
testUnion = empList;
8192

typescript-demo/tsconfig.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
1313
// "declaration": true, /* Generates corresponding '.d.ts' file. */
1414
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
15-
"sourceMap": true, /* Generates corresponding '.map' file. */
15+
"sourceMap": true /* Generates corresponding '.map' file. */,
1616
// "outFile": "./", /* Concatenate and emit output to single file. */
1717
"outDir": "./dist" /* Redirect output structure to the directory. */,
1818
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
@@ -60,8 +60,8 @@
6060
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
6161

6262
/* Experimental Options */
63-
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
64-
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
63+
"experimentalDecorators": true /* Enables experimental support for ES7 decorators. */,
64+
"emitDecoratorMetadata": true /* Enables experimental support for emitting type metadata for decorators. */,
6565

6666
/* Advanced Options */
6767
"skipLibCheck": true /* Skip type checking of declaration files. */,

0 commit comments

Comments
 (0)