-
Notifications
You must be signed in to change notification settings - Fork 3
/
example.ts
54 lines (39 loc) · 1.22 KB
/
example.ts
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
// eslint disable lowerCamelCase
import assert from 'assert'
import {
cloneClass,
instanceToClass,
} from '../src/mod'
class Employee {
public static company: string
constructor (
public name: string,
) {
}
public info () {
console.info(`Employee ${this.name}, Company ${(this.constructor as any).company}`)
}
}
console.info(`
# Example 1: cloneClass()
`)
const GoogleEmployee = cloneClass(Employee)
GoogleEmployee.company = 'Google'
const MicrosoftEmployee = cloneClass(Employee)
MicrosoftEmployee.company = 'Microsoft'
const employeeGg = new GoogleEmployee('Tom')
const employeeMs = new MicrosoftEmployee('Jerry')
employeeGg.info()
// Output: Employee Tom, Company Google
employeeMs.info()
// Output: Employee Jerry, Company Microsoft
console.info(`
# Example 2: instanceToClass()
`)
const RestoreGoogleEmployee = instanceToClass(employeeGg, Employee)
assert(RestoreGoogleEmployee === GoogleEmployee, 'Should get back the Class which instanciated the instance')
assert(RestoreGoogleEmployee !== Employee, 'Should be different with the parent Class')
const anotherEmployee = new RestoreGoogleEmployee('Mary')
anotherEmployee.info()
// Output: Employee Mary, Company Google
console.info()