mirror of
https://github.com/odoo/owl.git
synced 2025-10-06 19:59:41 +07:00
[IMP] component: allow using TypeScript type hints (#758)
This commit is contained in:
@@ -78,6 +78,19 @@ The `t-on` directive allows to prebind its arguments. For example,
|
||||
Here, `expr` is a valid Owl expression, so it could be `true` or some variable
|
||||
from the rendering context.
|
||||
|
||||
### Type Hinting
|
||||
|
||||
Note that if you work with Typescript, the `trigger` method is generic on the type of the payload.
|
||||
|
||||
You can then describe the type of the event, so you will see typing errors...
|
||||
|
||||
```typescript
|
||||
this.trigger<MyCustomPayload>('my-custom-event', payload);
|
||||
```
|
||||
```typescript
|
||||
myCustomEventHandler(ev: OwlEvent<MyCustomPayload>) { ... }
|
||||
```
|
||||
|
||||
## Inline Event Handlers
|
||||
|
||||
One can also directly specify inline statements. For example,
|
||||
|
||||
@@ -433,8 +433,8 @@ export class Component<Props extends {} = any, T extends Env = Env> {
|
||||
* up to the parent DOM nodes. Thus, it must be called between mounted() and
|
||||
* willUnmount().
|
||||
*/
|
||||
trigger(eventType: string, payload?: any) {
|
||||
this.__trigger(this, eventType, payload);
|
||||
trigger<T=any>(eventType: string, payload?: T) {
|
||||
this.__trigger<T>(this, eventType, payload);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
@@ -512,9 +512,9 @@ export class Component<Props extends {} = any, T extends Env = Env> {
|
||||
* Private trigger method, allows to choose the component which triggered
|
||||
* the event in the first place
|
||||
*/
|
||||
__trigger(component: Component, eventType: string, payload?: any) {
|
||||
__trigger<T>(component: Component, eventType: string, payload?: T) {
|
||||
if (this.el) {
|
||||
const ev = new OwlEvent(component, eventType, {
|
||||
const ev = new OwlEvent<T>(component, eventType, {
|
||||
bubbles: true,
|
||||
cancelable: true,
|
||||
detail: payload,
|
||||
|
||||
Reference in New Issue
Block a user