ts定义IProps类型,给Props(object类型的属性)赋值默认值的方式

文章讲述了如何在TypeScript中定义一个名为IPropsUpload的props接口,其中包括accept、multiple和options字段。options字段是一个UploadOptions对象,包含chunkSize和simultaneousUpload两个必填属性。为了给props的options赋默认值,文章推荐使用一个返回UploadOptions类型的函数,而不是直接使用对象字面量,因为后者会导致某些属性无法被识别。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

定义一个props的接口:

interface IPropsUpload {
    accept?: string,
    multiple?: boolean,
    options: UploadOptions,
}
// 其中options是一个对象,接收上传是自定义的一些options
type UploadOptions = {
    chunkSize: number,
    simultaneousUpload: number,
}

此时,定义一个props,再给props的options的两个必填属性赋初始默认值

const props = withDefaults(defineProps<IPropsUpload>(), {
    accept: '',
    multiple: false,
    options: () => ({
        chunkSize: 1024,
        simultaneousUpload: 3
    })
});

这样赋值之后,可以检测一下是否正确有效:
在这里插入图片描述
这里aa属性报错未定义,但是chunkSize属性未报错,由此可见我们定义的有效。

注意:options要使用一个返回UploadOptions类型函数的方式赋值,不要直接使用一个对象字面量赋值(像下面这样)

// ❌❌❌
const props = withDefaults(defineProps<IPropsUpload>(), {
    accept: '',
    multiple: false,
    options: {
        chunkSize: 1024,
        simultaneousUpload: 3
    }
});

在这里插入图片描述

// 数据 const getTotalBeforeData = function () { axiosPost("/user/total/SearchByDateAndName", param, t).then((res) => { // 表头名 const s1 = res.data.data.tableName.map((header: any, index: any) => ({ title: header, dataIndex: header, key: header, fixed: index < 4 ? "left" : undefined, // 固定前6列 width: index === 0 ? 50 : index === 2 ? 320 : 90, onHeaderCell: () => ({ style: { backgroundColor: '#769fcd' } }) }) ) s1[0].title = "no" s1[3].title = "実績工数" setColumns(s1) // 表内容 for (let index = 0; index < res.data.data.tableData.length; index++) { res.data.data.tableData[index].id = index + 1 } setDataSource(res.data.data.tableData) // 合计 const length = s1.length let s2: any = [] var totalKeys = Object.keys(res.data.data.statistics) var totalValues = Object.values(res.data.data.statistics) for (let index = 3; index < length; index++) { for (let i = 0; i < totalKeys.length; i++) { const totalKey = totalKeys[i]; const totalValue = totalValues[i]; if (totalKey == s1[index].key) { s2.push(totalValue); } } } setItems(s2) }); }// 姓名更改 const handleChangeName = (value: string[]) => { console.log(`selected ${value}`); param.name = value console.log(param) setSelectedName(value); }; // 定义重置函数 const handleResetAndQuery = () => { param = { name: [], date: '', }; setSelectedName([]); getTotalBeforeData(); };<Form.Item wrapperCol={{ offset: 6, span: 16 }}> <Button type="primary" size="large" htmlType="submit" onClick={getTotalBeforeData} > 查询 </Button> </Form.Item> </div> <div id='b2' style={{ display: "block" }} > <Form.Item wrapperCol={{ offset: 6, span: 16 }} style={{ marginLeft: "20px" }}> <Button type="primary" size="large" htmlType="submit" onClick={handleResetAndQuery} > 重置 </Button> </Form.Item>点击重置并没有重新查询并渲染
04-04
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值