Discriminated Unions

The SDKs natively support discriminated unions for both OpenAPI and Fern APIs.

1export type Shape = Triangle | Square;
2
3export interface Triangle {
4 type: "triangle";
5 a: number;
6 b: number;
7 c: number;
8}
9
10export interface Square {
11 type: "square";
12 side: number;
13}

Callers can create a Shape object by simply constructing the appropriate type. For example, creating a Triangle shape looks like the following:

1const shape: Shape = {
2 type: "triangle",
3 a: 3,
4 b: 4,
5 c: 5,
6};

Consumers can easily write branching logic by checking the discriminant.

1import { Shape } from "sdk";
2
3export function computeArea(shape: Shape): number {
4 if (shape.type === "triangle") {
5 // compute triangle area
6 } else if (shape.type === "square") {
7 // compute square area
8 }
9}