-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbasic.ts
62 lines (49 loc) · 1.47 KB
/
basic.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
55
56
57
58
59
60
61
62
import {create} from '../src/index';
interface User {
name: string;
age: number;
}
const users = create<User>().methods({
getAge() {
return this.age;
},
getName() {
return this.name;
},
isAdult() {
return this.age >= 18;
},
// Methods can take arguments, too!
isAtLeastAge(age: number) {
return this.age >= age;
},
exampleWithArrow: () => {
// This is an example of why you can't use an arrow function here, you must use
// method syntax to access .this (as that is how the method is bound)
// @ts-ignore
return this.name;
},
});
// Or, you can parse a direct object (for example if you want to do schema valdiation first)
// and use .from: `const alistair = users.from({ name: 'alistair', age: 17 });`
const alistair = users.parse(JSON.stringify({name: 'alistair', age: 17}));
// Accessing property .name from raw object and also our method .getAge() & .isAdult()
console.log(
alistair.name,
'is',
alistair.getAge(),
'is adult?:',
alistair.isAdult(),
);
console.log('Alistair is over 16?:', alistair.isAtLeastAge(16));
// Stringifing will result in the original object JSON you parsed in
console.log(JSON.stringify(alistair));
// Proof that the proxy behaves like a regular object
// when getting keys...
console.log('Keys:', Object.getOwnPropertyNames(alistair));
// ...and when iterating keys
for (const key in alistair) {
console.log(key);
}
// Stringifying the proxy will result in the dreaded [object Object]
console.log(`Stringifying ${alistair}`);