先上代码:
type DetailDataType = {
detailType: 'type1'
params: {
a1: string
test1: number
}
} | {
detailType: 'type2'
params: {
ss: Array<string>
aa: Record<string, boolean>
}
}
type dataType = { id: string } & DetailDataType
const testFn = <Type extends dataType['detailType']>(type: Type, data: unknown) => {
if (type === 'type1') {
const { a1, test1 } = data;
}
if (type === 'type2') {
const { ss, aa } = data;
}
};
testFn('type1', { a1: '', test1: 1 });
testFn('type1', { ss: [], aa: { test: false } });
问题:
该如何定义函数testFn的data参数,才能让data的解构与DetailDataType的定义相符,并且编辑器有提示,现在data的解构全是any,如下:
求ts大神解答,谢谢!!!