Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(typescript): Add support explicit null values #5612

Open
wants to merge 21 commits into
base: main
Choose a base branch
from

Conversation

amckinney
Copy link
Member

@amckinney amckinney commented Jan 14, 2025

Now that the Fern definition supports nullable properties (re: #5601), this upgrades the TypeScript generator to IRv55 to support this feature. For example, consider the following Fern definition:

types:
  User:
    properties:
      name: string
      tags: nullable<list<string>>
      metadata: optional<nullable<Metadata>>

  Metadata:
    properties:
      createdAt: datetime
      updatedAt: datetime
      avatar: nullable<string>
      activated: optional<nullable<boolean>>

The generated TypeScript types look like the following:

export interface User {
    name: string;
    tags: string[] | null;
    metadata?: Metadata | null;
}

export interface Metadata {
    createdAt: Date;
    updatedAt: Date;
    avatar: string | null;
    activated?: boolean | null;
}

Note that this also temporarily disables the dynamic snippet tests so that we can more confidently test the TypeScript SDK generator until example generation is fixed.

@amckinney amckinney requested a review from dsinghvi as a code owner January 14, 2025 18:34
Comment on lines +645 to +646
nullable: (optionalType) =>
this.getTemplateFromTypeReference({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

optionalType => nullableType

@@ -405,6 +408,7 @@ export class GeneratedThrowingEndpointResponse implements GeneratedEndpointRespo
literal: () => undefined,
list: () => undefined,
set: () => undefined,
nullable: (optType) => this.getDefaultPaginationValue({ type: optType }),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

optType => nullableType

const _queryParams: Record<string, string | string[] | object | object[] | null> = {};
if (usernames != null) {
if (Array.isArray(usernames)) {
_queryParams["usernames"] = usernames.map((item) => item);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unrelated, but are we creating a copy of an array here? I think there are simpler ways to do this.

Copy link

github-actions bot commented Jan 15, 2025

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants