-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path3-generate-outputs.ts
50 lines (42 loc) · 1.24 KB
/
3-generate-outputs.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
/**
* Example 3: map type, with name extended by prefix/suffix
*/
import { Component, EventEmitter, Input, Output } from '@angular/core';
type User = {
name: string;
surname: string;
age: number;
};
// Lets create handled which will map this type to new one
// and extend it with `Changed` fields, which are Angular's event emitters.
type Handlers<T> = T & {
[Key in keyof T as `${string & Key}Changed`]: EventEmitter<T[Key]>;
};
type UserWithHandlers = Handlers<User>;
/**
* Result type 👇
*
* type UserWithHandlers = User & {
nameChanged: EventEmitter<string>;
surnameChanged: EventEmitter<string>;
ageChanged: EventEmitter<number>;
}
*/
/**
* Example usage.
* Written by hand is a little bit meh, but think about
* generating components. Then this kind of generic helper shows its power 💪
*/
@Component({
selector: 'mm-user',
template: `<span role="img">👋</span>`,
})
class UserComponent implements UserWithHandlers {
@Input() name: string;
@Input() surname: string;
@Input() age: number;
@Output() nameChanged: EventEmitter<string> = new EventEmitter();
@Output() surnameChanged: EventEmitter<string> = new EventEmitter();
@Output() ageChanged: EventEmitter<number> = new EventEmitter();
}
export {};