简体   繁体   中英

Using OR in type interface typescript

I have an array in which every index item can be either IBusinessDetailCompetitor or ICustomerCompetitorInstant

So can I do something ilke this
customerCompititorArray: (IBusinessDetailCompetitor[] | ICustomerCompetitorInstant[])

But this is giving errror. Please help.

The item in array should be union types :

interface IBusinessDetailCompetitor {
    a: string
}
interface ICustomerCompetitorInstant {
    b: string
}

const customerCompititorArrayCorrect: Array<IBusinessDetailCompetitor | ICustomerCompetitorInstant> = [{a: '1'}, {b: '2'}]

See TypeScript Playground

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM