TypeScript require one parameter or the other, but not neither

前端 未结 4 514
不思量自难忘°
不思量自难忘° 2021-01-04 01:50

Say I have this type:

export interface Opts {
  paths?: string | Array,
  path?: string | Array

        
4条回答
  •  北荒
    北荒 (楼主)
    2021-01-04 02:12

    You may use

    export type Opts = { path: string | Array } | { paths: string | Array }
    

    To increase readability you may write:

    type StringOrArray = string | Array;
    
    type PathOpts  = { path : StringOrArray };
    type PathsOpts = { paths: StringOrArray };
    
    export type Opts = PathOpts | PathsOpts;
    

提交回复
热议问题