vuejs / rfcs

RFCs for substantial changes / feature additions to Vue core
4.85k stars 549 forks source link

Infer attrs for implementing HOCs #477

Open rudyxu1102 opened 1 year ago

rudyxu1102 commented 1 year ago

Summary

Allowing to infer attrs by using attrs option of defineComponent or defineCustomElement. And in the setup-script, passing generic type in the defineAttrs<T> will also infer attrs to T.

defineComponent

const Comp = defineComponent({
  props: {
    foo: String
  },
  attrs: Object as AttrsType<{
    bar?: number
  }>,
  created() {
    this.$attrs.bar // number | undefined
  }
});

<Comp foo={'str'} bar={1} />;

Setup Script

<script setup lang="ts">
const attrs = defineAttrs<{ bar?: number }>();
</script>

Links

Important: Do NOT comment on this PR. Please use the discussion thread linked above to provide feedback, as it provides branched discussions that are easier to follow.This also makes the edit history of the PR clearer.