Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TS: types for custom event is hard to infer #107

Open
xcatliu opened this issue Oct 23, 2018 · 2 comments
Open

TS: types for custom event is hard to infer #107

xcatliu opened this issue Oct 23, 2018 · 2 comments

Comments

@xcatliu
Copy link
Collaborator

xcatliu commented Oct 23, 2018

Omi use this syntax to create a custom event:

// trigger CustomEvent
this.fire('abc', { name: 'dntzhang', age: 12 })

This is very flexible, but it brings difficulties to type inference.
Specifically, how can parent component know it has a onAbc props? What is the type of evt.detail?

@volodymyrlut
Copy link

You can resolve this issue like you would normally do in, for example, react on typescript - declaring an Interface and relying on it at parent's component.

@xcatliu
Copy link
Collaborator Author

xcatliu commented Oct 25, 2018

@volodymyrlut You are right, we can write an interface to describe the onAbc, then parent component can rely on it.
However, in React, child component can also rely on it:

interface ChildProps {
  onAbc: (n: number) => string;
}

class Child extends React.Component<ChildProps> {
  doSomething() {
    // Here ts know that onAbc should pass an argument with number type
    this.props.onAbc(200);
  }
}
interface ChildProps {
  onAbc: (n: number) => string;
}

class Child extends Omi.WeElement<ChildProps> {
  doSomething() {
    // It's hard to know that if the first argument is string 'abc', then the second argument needs to be a number
    this.fire('abc', 200);
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants