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

feat(types): support inferring attrs #7444

Open
wants to merge 20 commits into
base: minor
Choose a base branch
from

Conversation

rudyxu1102
Copy link
Contributor

@rudyxu1102 rudyxu1102 commented Jan 1, 2023

Related

RFC

Specific scenes

1. Wrapping an html element

import { defineComponent, type ImgHTMLAttributes, type AttrsType } from 'vue';

const MyImg = defineComponent({
    props: {
        foo: String
    },
    attrs: Object as AttrsType<ImgHTMLAttributes>,
    created() {
        this.$attrs.class // any
        this.$attrs.style // StyleValue | undefined
        this.$attrs.onError // ((payload: Event) => void) | undefined
        this.$attrs.src // string | undefined
    },
    render() {
        return <img {...this.$attrs} />
    }
});

<MyImg class={'str'} style={'str'} src={'https://xxx.com/aaa.jpg'} onError={(e) => {
  e; // Event
}}/>;

2. Wrapping a Vue component

import { defineComponent, type AttrsType } from 'vue';

const Comp = defineComponent({
    props: {
        foo: String
    },
    emits: {
        baz: (val: number) => true
    },
    render() {
        return <div>{this.foo}</div>
    }
});

const MyComp = defineComponent({
    props: {
        bar: Number
    },
    attrs: Object as AttrsType<typeof Comp>,
    created() {
        this.$attrs.class // unknown
        this.$attrs.style // unknown
        this.$attrs.onBaz; // ((val: number) => any) | undefined
        this.$attrs.foo; // string | undefined
    },
    render() {
        return <Comp {...this.$attrs} />
    }
});

<MyComp class={'str'} style={'str'} bar={1} foo={'str'} onBaz={(val) => { 
    val; // number
}} />;

@netlify
Copy link

netlify bot commented Jan 1, 2023

Deploy Preview for vuejs-coverage failed.

Name Link
🔨 Latest commit cc506ce
🔍 Latest deploy log https://app.netlify.com/sites/vuejs-coverage/deploys/63b5a24c5e5c910008a149fe

@rudyxu1102 rudyxu1102 marked this pull request as draft January 1, 2023 11:33
@rudyxu1102 rudyxu1102 marked this pull request as ready for review January 1, 2023 16:06
@rudyxu1102 rudyxu1102 changed the title feat(types): infer attrs feat(types): support attrs inferred Jan 2, 2023
@rudyxu1102 rudyxu1102 changed the title feat(types): support attrs inferred feat(types): support inferring attrs Jan 2, 2023
@netlify
Copy link

netlify bot commented Jan 4, 2023

Deploy Preview for vue-sfc-playground failed.

Name Link
🔨 Latest commit cc506ce
🔍 Latest deploy log https://app.netlify.com/sites/vue-sfc-playground/deploys/63b5a24c075f5b0007a104a6

@productdevbook
Copy link
Contributor

@yyx990803 we need this in oku-ui, for example we need to go back the

element types so that volar can suggest user auto-suggestion with both props and attrs types.

can you review and include this?

@sxzz
Copy link
Member

sxzz commented Aug 23, 2023

Looks great. Could you please rebase your branch to resolve conflicts?

I guess this feature will more be like defineSlots. Without adding runtime code, but adding only TS types.

packages/compiler-sfc/src/compileScript.ts Outdated Show resolved Hide resolved
packages/runtime-core/src/apiSetupHelpers.ts Outdated Show resolved Hide resolved
@github-actions
Copy link

github-actions bot commented Aug 30, 2023

Size Report

Bundles

File Size Gzip Brotli
runtime-dom.global.prod.js 85.8 kB 32.6 kB 29.5 kB
vue.global.prod.js 132 kB 49.3 kB 44.3 kB

Usages

Name Size Gzip Brotli
createApp 47.9 kB 18.8 kB 17.2 kB
createSSRApp 50.6 kB 19.9 kB 18.2 kB
defineCustomElement 50.3 kB 19.6 kB 17.9 kB
overall 61.2 kB 23.7 kB 21.6 kB

@rudyxu1102 rudyxu1102 marked this pull request as draft August 31, 2023 01:54
@rudyxu1102 rudyxu1102 marked this pull request as ready for review September 1, 2023 05:27
@rudyxu1102 rudyxu1102 requested a review from sxzz September 1, 2023 05:27
@sxzz sxzz changed the base branch from main to 3.4 September 1, 2023 12:10
@rudyxu1102
Copy link
Contributor Author

Could you please REBASE your branch due the minor branch has been also rebased.

@sxzz done

@amritk
Copy link

amritk commented Nov 8, 2023

This is much needed, thanks!

@jd-solanki
Copy link

@edison1105 Is this accidental?

@edison1105
Copy link
Member

@edison1105 Is this accidental?

It will be reopen soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Needs Review
Status: Done
9 participants