Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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, but the rest of my code benefits if I can define it as described instead.

interface IModal {
    content?: string;
    content_object?: object;
}
like image 264
jas7457 Avatar asked May 10 '18 22:05

jas7457


Video Answer


1 Answers

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.

like image 187
Ryan Cavanaugh Avatar answered Oct 22 '22 07:10

Ryan Cavanaugh