2
type ChartType = 'bar' | 'line' | 'pie';


interface PropsType {
  type: EChartType;
  xData: **** ;
  seriesData: ****  ;
}
const props = defineProps<PropsType>();


how to do

When ChartType = 'bar', xData has type string[] and seriesData has type number[]. When ChartType = 'line', xData has type Array<string|number> and seriesData has type string[]. When ChartType = 'pie', xData has type number[] and seriesData has type {value:string,name:string}[].

Giorgi Moniava
  • 27,046
  • 9
  • 53
  • 90
laterday
  • 353
  • 1
  • 4
  • 11
  • Does this answer your question? [Typescript: Type of a property dependent on another property within the same object](https://stackoverflow.com/questions/56949513/typescript-type-of-a-property-dependent-on-another-property-within-the-same-obj) – Giorgi Moniava Oct 23 '22 at 08:14
  • I don't know how to write it, can you help me implement it – laterday Oct 23 '22 at 08:19
  • Please read about [Discriminated unions](https://www.typescriptlang.org/docs/handbook/typescript-in-5-minutes-func.html#discriminated-unions) – captain-yossarian from Ukraine Oct 24 '22 at 08:27

1 Answers1

3

This is the answer to your question.

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}[]
    }
Avetik Nersisyan
  • 327
  • 3
  • 13