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

[TSX] deny unknown attributes by default #1519

Closed
wonderful-panda opened this issue Jul 6, 2020 · 0 comments
Closed

[TSX] deny unknown attributes by default #1519

wonderful-panda opened this issue Jul 6, 2020 · 0 comments

Comments

@wonderful-panda
Copy link
Contributor

What problem does this feature solve?

Currently, unknown attributes are allowed in component tag.

For example, this code is valid.

const MyComponent = defineComponent({
  props: { msg: String },
  setup(props) {
    return () => <div>{props.msg}</div>;
  }
});

const ParentComponent = defineComponent({
  setup() {
    return () => <MyComponent msg="Hello" foo={1} />; // foo is not defined, but allowed.
  }
});

But this breaks type-safety (for example, TypeScript can't detect misspelling of optional prop name), and there is no way to disable this.

I think it would be nice if unknown attributes are denied by default, and some escape-hatches are provided.

What does the proposed API look like?

In TypeScript, deny unknown attributes by default.

And allow to pass unknown attributes via special attribute (something like attrs)

const ParentComponent = defineComponent({
  setup() {
    return () => <MyComponent msg="Hello" attrs={{ foo: 1 }} />; 
  }
});

And provide optional type definition like below. Unknown attributes are allowed again by importing this.

 declare module '@vue/runtime-core' {
    interface ComponentCustomOptions {
       [key: string]: any;
    }
}
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants