-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
Copy pathcomputed.ts
executable file
·100 lines (81 loc) · 2.76 KB
/
computed.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import Ember from 'ember';
import { expectTypeOf } from 'expect-type';
function customMacro(message: string) {
return Ember.computed(() => {
return [message, message];
});
}
class Person extends Ember.Object {
firstName = '';
lastName = '';
age = 0;
// Equivalent to a per-instance `defineProperty` call.
@Ember.computed()
get noArgs() {
return 'test';
}
@Ember.computed('firstName', 'lastName')
get fullName(): string {
return `${this.get('firstName')} ${this.get('lastName')}`;
}
@(Ember.computed('fullName').readOnly())
get fullNameReadonly() {
return this.get('fullName');
}
@Ember.computed('firstName', 'lastName')
get fullNameWritable(): string {
return this.get('fullName');
}
set fullNameWritable(value: string) {
const [first, last] = value.split(' ');
this.set('firstName', first);
this.set('lastName', last);
}
@(Ember.computed().meta({ foo: 'bar' }).readOnly())
get combinators() {
return this.get('firstName');
}
@customMacro('hi')
declare hiTwice: string[];
}
const person = Person.create({
firstName: 'Fred',
lastName: 'Smith',
age: 29,
});
expectTypeOf(person.firstName).toEqualTypeOf<string>();
expectTypeOf(person.age).toEqualTypeOf<number>();
expectTypeOf(person.noArgs).toEqualTypeOf<string>();
expectTypeOf(person.fullName).toEqualTypeOf<string>();
expectTypeOf(person.fullNameReadonly).toEqualTypeOf<string>();
expectTypeOf(person.fullNameWritable).toEqualTypeOf<string>();
expectTypeOf(person.combinators).toEqualTypeOf<string>();
expectTypeOf(person.get('firstName')).toEqualTypeOf<string>();
expectTypeOf(person.get('age')).toEqualTypeOf<number>();
expectTypeOf(person.get('noArgs')).toEqualTypeOf<string>();
expectTypeOf(person.get('fullName')).toEqualTypeOf<string>();
expectTypeOf(person.get('fullNameReadonly')).toEqualTypeOf<string>();
expectTypeOf(person.get('fullNameWritable')).toEqualTypeOf<string>();
expectTypeOf(person.get('combinators')).toEqualTypeOf<string>();
expectTypeOf(person.getProperties('firstName', 'fullName', 'age')).toMatchTypeOf<{
firstName: string;
fullName: string;
age: number;
}>();
const person2 = Person.create({
fullName: 'Fred Smith',
});
expectTypeOf(person2.get('firstName')).toEqualTypeOf<string>();
expectTypeOf(person2.get('fullName')).toEqualTypeOf<string>();
const person3 = Person.extend({
firstName: 'Fred',
fullName: 'Fred Smith',
}).create();
expectTypeOf(person3.get('firstName')).toEqualTypeOf<string>();
expectTypeOf(person3.get('fullName')).toEqualTypeOf<string>();
const person4 = Person.extend({
firstName: Ember.computed(() => 'Fred'),
fullName: Ember.computed(() => 'Fred Smith'),
}).create();
expectTypeOf(person4.get('firstName')).toEqualTypeOf<string>();
expectTypeOf(person4.get('fullName')).toEqualTypeOf<string>();