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

[Type Generation]: Explicitly Mark Optional TypeScript Properties Using a ? #573

Open
ITenthusiasm opened this issue Mar 10, 2024 · 0 comments

Comments

@ITenthusiasm
Copy link

The Problem

Currently, when generating TS files from SQL queries, the TS types require nullable parameters to be specified. For example

/* 
  @name createAddress
  @param address -> (line1!, line2, city!, state!, zipcode!)
*/
INSERT INTO addresses (line1, line2, city, state, zipcode)
VALUES :address!
RETURNING id;

Generates the following type

/** 'CreateAddress' parameters type */
export interface ICreateAddressParams {
  address: {
    line1: string,
    line2: string | null | void,
    city: string,
    state: number,
    zipcode: string
  };
}

Here, line2 is properly identified as string | undefined | null, but the property itself is not properly marked as optional. Consequently, if I supply an object to createAddress.run that has an optional line2 property, the TypeScript compiler complains even though everything is fine at runtime.

The Solution

I think the simple fix would be to let TypeScript know that nullable parameters/properties are optional. The way to do that would be with a ? So ideally, the following would be generated for optional properties

/** 'CreateAddress' parameters type */
export interface ICreateAddressParams {
  address: {
    // ... Other Properties
    line2?: string | null | void, // notice the `?`
  };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant