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

typescript中,form表单的ref类型问题 #2677

Closed
LannyChung opened this issue Nov 20, 2023 · 2 comments · Fixed by #2923
Closed

typescript中,form表单的ref类型问题 #2677

LannyChung opened this issue Nov 20, 2023 · 2 comments · Fixed by #2923
Labels
Milestone

Comments

@LannyChung
Copy link

NutUI 包名

@nutui/nutui

NutUI 版本号

4.2.0

平台

h5

重现链接

重现步骤

`

登录

登录
path: / <script setup lang="ts"> const formRef = ref(null); const formData = reactive({ account: '', password: '' });

const rules = {
account: [
{ required: true, message: '请输入账号'},
],
password: [
{ required: true, message: '请输入密码'},
]
};

const submit = () => {
// @ts-ignore
formRef.value.validate().then(({ valid, errors }) => {
if (valid) {
console.log('success');
} else {
console.log('error', errors);
}
})
}

</script>`

我在form表单上绑定了ref变量formRef,声明变量时,应该传入其泛型,以便调用其validate等方法时不会报错,我尝试从@nutui/nutui中寻找导出的form表单类型,但是并没有找到,此组件库是否不提供类型的导出

期望的结果是什么?

通过泛型处理form表单的类型,而不是使用 // @ts-ignore忽略typescript的报错

实际的结果是什么?

使用 // @ts-ignore忽略typescript的报错

环境信息

No response

其他补充信息

{
"name": "manager",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "vue-tsc && vite build",
"preview": "vite preview"
},
"dependencies": {
"@nutui/nutui": "^4.2.0",
"amfe-flexible": "^2.2.1",
"vue": "^3.3.4"
},
"devDependencies": {
"@types/node": "^20.8.9",
"@types/postcss-pxtorem": "^6.0.2",
"@vitejs/plugin-vue": "^4.2.3",
"postcss": "^8.4.31",
"postcss-pxtorem": "^6.0.0",
"typescript": "^5.0.2",
"unplugin-auto-import": "^0.16.7",
"unplugin-vue-components": "^0.25.2",
"vite": "^4.4.5",
"vite-plugin-pages": "^0.31.0",
"vite-plugin-vue-layouts": "^0.8.0",
"vue-tsc": "^1.8.5"
}
}

@eiinu eiinu added the future someday label Nov 20, 2023
@ntnyq
Copy link
Contributor

ntnyq commented Dec 2, 2023

可以通过 InstanceType 获取组件类型,不过组件库提供导出更方便点。

<template>
  <nut-form ref="formRef">
    <nut-button @click="handleSubmit">提交</nut-button>
  </nut-form>
</template>

<script lang="ts" setup>
import { Form } from '@nutui/nutui-taro'

type FormInstance = InstanceType<typeof Form>

const formRef = ref<FormInstance>()

const handleSubmit = async () => {
  try {
    await formRef.value?.validate()  
  } catch (err) {
    console.log(err)
  }
}
</script>

@eiinu
Copy link
Member

eiinu commented Dec 14, 2023

在目前的组件库构建方式下,没有能力扩展出新的类型导出字段。
最近在测试对这部分逻辑的改造方案。

@eiinu eiinu added typing and removed future someday labels Jan 22, 2024
@eiinu eiinu added this to the v4.3.1 milestone Jan 26, 2024
@eiinu eiinu linked a pull request Feb 13, 2024 that will close this issue
21 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants