简体   繁体   中英

Typescript Interface: Exactly one optional parameter is required

I'd like to define an interface that allows you to supply content OR content_object but not both. You have to define one or the other. What is the simplest way to achieve this in TypeScript? I know I could say that content is string | object string | object , but the rest of my code benefits if I can define it as described instead.

interface IModal {
    content?: string;
    content_object?: object;
}
type IModal = { content: string; content_object?: undefined } |
              { content_object: object; content?: undefined }

This answer contains only code and is therefore bad according to automated systems.

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