vuejs / babel-plugin-jsx

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

[BUG] v-model when props has attr `required: true`, typescript report errors. #636

Open chaozwn opened 1 year ago

chaozwn commented 1 year ago

🐛 Bug description

v-model when props has attr required: true, typescript report errors. link: #151 image

🏞 Desired result

🚑 Other information

funny-family commented 1 year ago

@chaozwn, can you provide code of ShedulerDialog component?

chaozwn commented 1 year ago

@chaozwn, can you provide code of ShedulerDialog component?

import { CellGroup, Dialog, Field, Switch } from "vant"; import { defineComponent, ref, toRaw, toRef, toRefs } from "vue"; import type { PropType } from "vue"; import type { ShowDialogType, Task } from "@/views/Home/hook";

const SchedulerDialog = defineComponent({ props: { show: { type: Boolean, default: false, }, showDialog: { type: Object as PropType, required: true, }, task: { type: Object as PropType, required: true, }, }, emits: ["onUpdate:show", "onUpdate:task"], setup(props, { emit }) { const { showDialog, task } = toRefs(props); const handleOnConfirm = () => { console.log(task.value); emit("onUpdate:task", task.value); }; return () => (

{{ input: () => ( ), }}
);

}, });

export default SchedulerDialog;

funny-family commented 1 year ago

@chaozwn

  1. props related to v-model should be required: false (I see no reason to make it required: true)
  2. try this <SchedulerDialog v-model={[yourValue, 'task']} />
chaozwn commented 1 year ago

@chaozwn

  1. props related to v-model should be required: false (I see no reason to make it required: true)
  2. try this <SchedulerDialog v-model={[yourValue, 'task']} />

but it works in vue template. i think it should not be throw a TSLint problem right?

chaozwn commented 1 year ago

@chaozwn

  1. props related to v-model should be required: false (I see no reason to make it required: true)
  2. try this <SchedulerDialog v-model={[yourValue, 'task']} />

in typescript,when props has required: true, ts can not check v-model:task, i found that be seen as a whole attr.

funny-family commented 1 year ago

@chaozwn

  1. props related to v-model should be required: false (I see no reason to make it required: true)
  2. try this <SchedulerDialog v-model={[yourValue, 'task']} />

but it works in vue template. i think it should not be throw a TSLint problem right?

TSLint? As far as I know it is depricated. So, vue templates work very bad with ts.

funny-family commented 1 year ago

You can check here an example of component with v-model

rcjiang commented 1 year ago

This problem should be solved, instead of avoiding it using methods such as required: false

sxzz commented 1 year ago

v-model:foo is a syntax sugar from Vue, and it's not supported by TS. The only way we can provide now is to use foo={something} onUpdate:foo={handler}

zhiyuanzmj commented 1 year ago

I created a PR https://github.com/vue-macros/vue-macros/pull/494 to resolved it.

// tsconfig.json
{
  "vueCompilerOptions": {
    "target": 3,
    "plugins": [
      "@vue-macros/volar/jsx-directive"
      // ...more feature
    ]
  }
}
lixiaofa commented 11 months ago

I also face the same problem, how should I solve it @chaozwn @sxzz @funny-family @zhiyuanzmj @rcjiang

zhiyuanzmj commented 11 months ago

I also face the same problem, how should I solve it @chaozwn @sxzz @funny-family @zhiyuanzmj @rcjiang

https://vue-macros.dev/guide/bundler-integration.html

You can install @vue-macros/volar plugin to resolve it

pnpm add @vue-macros/volar

Usage

// tsconfig.json
{
  "vueCompilerOptions": {
    "plugins": [
      // ...
      "@vue-macros/volar/jsx-directive"
    ]
  }
}
lixiaofa commented 11 months ago
@vue-macros/volar

Thank you !

lixiaofa commented 11 months ago

<script setup lang="tsx"> <FsInputCell v-model={[rowData, 'rowData']} column={column} /> </script> export type InputCellProps = { rowData: any column: Column } export type InputCellEmits = { 'onUpdate:rowData': [rowData: any] }

emit('onUpdate:rowData', updataRowData) image There will be a warning

@sxzz @rcjiang @zhiyuanzmj @chaozwn @funny-family

zhiyuanzmj commented 11 months ago

You can use defineEmits or defineModel in the component.

// without 'on' prefix 
defineEmits([ 'update:rowData'])
// Or
defineModel('rowData')

If the issue still unresolved, please provide a minimal repository. By the way, you don't need to @ everyone, we can also see your message.

lixiaofa commented 11 months ago

You can use defineEmits or defineModel in the component.

// without 'on' prefix 
defineEmits([ 'update:rowData'])
// Or
defineModel('rowData')

If the issue still unresolved, please provide a minimal repository. By the way, you don't need to @ everyone, we can also see your message.

const emit = defineEmits<{ 'update:rowData': [rowData: any] }>() Why type declaration doesn't work?

zhiyuanzmj commented 11 months ago

Type declaration should also be possible, if not, please provide a minimal repository.

lixiaofa commented 11 months ago

Type declaration

@zhiyuanzmj see https://github.com/lixiaofa/test636/blob/main/vite.config.ts

step: 1.click the cell, then input cell 2.Click anywhere

zhiyuanzmj commented 11 months ago

Thank's for your repository, It seems to be caused by vue-macros, I'll fix it later. You can setting shortEmit: true temporarily fixes it.

 VueMacros({
      shortEmits: true,
      plugins: {
        vue: vue(),
        vueJsx: vueJsx() // 如果需要
      }
  }),
lixiaofa commented 11 months ago
shortEmits

image

image

It doesn't seem to be effective @zhiyuanzmj

zhiyuanzmj commented 11 months ago

Sorry, It's be caused by 'betterDefine', waiting for merge https://github.com/vue-macros/vue-macros/pull/582.

lixiaofa commented 11 months ago

Sorry, It's be caused by 'betterDefine', waiting for merge vue-macros/vue-macros#582.

Okay, thank you for your help