File tree 1 file changed +5
-3
lines changed
1 file changed +5
-3
lines changed Original file line number Diff line number Diff line change @@ -96,7 +96,10 @@ class _Nested {
96
96
}
97
97
98
98
// Enhance the class by specifying the path to nested events
99
- const Nested = AddEvents <typeof _Nested , NestedEvents >(_Nested , ' eventsContainer' );
99
+ export const Nested = AddEvents <typeof _Nested , NestedEvents >(_Nested , ' eventsContainer' );
100
+
101
+ // Export it as a type as well, to avoid "'Nested' refers to a value, but is being used as a type".
102
+ export const Nested = _Nested & WithEventsDummyType (NestedEvents );
100
103
101
104
// Create an instance and attach event listeners
102
105
const instance = new Nested ();
@@ -114,15 +117,14 @@ Here’s how errors are managed when an incorrect path is specified:
114
117
115
118
``` typescript
116
119
try {
117
- const InvalidClass = AddEvents <typeof Nested , NestedEvents >(Nested , ' incorrectPath' );
120
+ const InvalidClass = AddEvents <typeof _Nested , NestedEvents >(_Nested , ' incorrectPath' );
118
121
const wrongInstance = new InvalidClass ();
119
122
wrongInstance .on (' eventOne' , () => console .log (' This will never run' ));
120
123
} catch (error ) {
121
124
console .error (error ); // Outputs: Error: Event "incorrectPath.eventOne" is not defined
122
125
}
123
126
```
124
127
125
-
126
128
## FAQ
127
129
##### _ Rather than requiring constraints on the type ` U ` in AddEvents via documentation, why not express them in the type system?_
128
130
You can’t perform that action at this time.
0 commit comments