vuejs / babel-plugin-jsx

JSX for Vue 3
https://vue-jsx-explorer.netlify.app
MIT License
1.71k stars 142 forks source link

[Question] In the future and Object slot typing be supported by this plugin #651

Open louiss0 opened 1 year ago

louiss0 commented 1 year ago

🧐 Problem Description

In Vue you are able to inject slots by using this syntax

<HelloWorld>
      { {
          default(props:Record<string, unknown>) {

            return props? props.value : "No props"

          }
        }
      }
 </HelloWorld>

But when I give types for slots I get no error regarding whether or not I'm typing the wrong one or not.


type Slots = SlotsType< { default(props: Record<string, unknown>): JSX.Element }>
export default defineComponent((props: { msg: string, value: number, }, ctx: SetupContext<null, Slots>) => {

 return <> {ctx.slots.default() } </>

}

🚑 Other information

Here is a repo You can use to get what I mean. Vue JSX Example

funny-family commented 1 year ago

@louiss0, it is much easier to use v-slots directive, if you want strong typing.

Examples: https://github.com/funny-family/vue3-ts-playground/blob/master/src/app/components/not-found/not-found.component.tsx#L210 https://github.com/funny-family/vue3-ts-playground/blob/master/src/app/shared/components/text-field/text-field.render.tsx

louiss0 commented 1 year ago

@louiss0, it is much easier to use v-slots directive, if you want strong typing.

Examples: https://github.com/funny-family/vue3-ts-playground/blob/master/src/app/components/not-found/not-found.component.tsx#L210 https://github.com/funny-family/vue3-ts-playground/blob/master/src/app/shared/components/text-field/text-field.render.tsx

You can use this Stackblits example repo to show me what you mean. Stack Blits Setup for you

zhiyuanzmj commented 2 months ago

https://github.com/vue-macros/vue-macros/pull/720 vue-macros support the type hint for v-slots

louiss0 commented 2 months ago

vue-macros/vue-macros#720 vue-macros support the type hint for v-slots

I need to write an object as the child not use a directive.

<HelloWorld>
      { 
          default(props:Record<string, unknown>) {

            return props? props.value : "No props"

          }
        }

 </HelloWorld>
zhiyuanzmj commented 2 months ago

Because of the child maybe a variable to display string.

const foo = 'hello world'
<HelloWorld>
      { foo }
 </HelloWorld>
louiss0 commented 2 months ago

Because of the child maybe a variable to display string.

const foo = 'hello world'
<HelloWorld>
      { foo }
 </HelloWorld>

Can the plugin be rewritten so that JSX works like React or Solid? I should be able to use an object as a child or the template tag to define slots like in SFC. If I specify types in the function I should be forced to type an object as a child.

zhiyuanzmj commented 2 months ago

Vue Macros have a v-slot directive to define slot just like in vue template, also has type hint. https://vue-macros.dev/features/jsx-directive.html#v-slot

zhiyuanzmj commented 2 months ago

Maybe I should add type hints only if the expression is written as an object.

louiss0 commented 2 months ago

Maybe I should add type hints only if the expression is written as an object.

The plugin needs to be rewritten to support objects as child without using an expression.

zhiyuanzmj commented 2 months ago

https://github.com/vue-macros/vue-macros/pull/722

zhiyuanzmj commented 2 months ago

@vue-macros/volar@2.3.1 is released.