Skip to content

Using optional fields rather than | undefined #184

@seanlafferty-ibm

Description

@seanlafferty-ibm

The generated types look like so

export type ListSitesRequest = {
  // Behaviors: REQUIRED
  parent: string | undefined;
  // Behaviors: OPTIONAL
  pageSize: number | undefined;
  // Behaviors: OPTIONAL
  pageToken: string | undefined;
};

which means typescript will make you initialize each field, even if you intend to keep it undefined

const a: ListSitesRequest = {parent: "foo/bar"}; // Type '{ parent: string; }' is missing the following properties from type 'ListSitesRequest': pageSize, pageToken
const a: ListSitesRequest = {parent: "foo/bar", pageSize: undefined, pageToken: undefined}; // passes

Alternatively we could generate the optional fields this way:

export type ListSitesRequest = {
  // Behaviors: REQUIRED
  parent?: string;
  // Behaviors?=: OPTIONAL
  pageSize?: number;
  // Behaviors: OPTIONAL
  pageToken?: string;
};

which would allow field omission for fields that the caller does not want to supply.

Is the current behavior a stylistic choice, or would we be open to changing it?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions