type ChartType = 'bar' | 'line' | 'pie';
interface PropsType {
type: EChartType;
xData: **** ;
seriesData: **** ;
}
const props = defineProps<PropsType>();
怎么做
何时 ChartType = 'bar'
, xData
类型为string[]
和seriesData
类型为number[]
。
当ChartType = 'line'
, xData
类型为Array<string|number>
和seriesData
类型为string[]
。
当ChartType = 'pie'
, xData
类型为number[]
和seriesData
类型为{value:string,name:string}[]
.
最佳答案
这就是您问题的答案。
type TPropsType = {
type: "bar"
xData: string[]
seriesData: number[]
} | {
type: "line"
xData: Array<string|number>
seriesData: string[]
} | {
type: 'pie'
xData: number[]
seriesData: {value:string,name:string}[]
}
关于typescript 如何从一个参数的类型推断另一个参数的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74169555/