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

kyselyType annotation: support types defined in other modules #61

Closed
handymenny opened this issue Jul 15, 2023 · 4 comments
Closed

kyselyType annotation: support types defined in other modules #61

handymenny opened this issue Jul 15, 2023 · 4 comments

Comments

@handymenny
Copy link

handymenny commented Jul 15, 2023

I noticed that kyselyType annotation doesn't work with types defined in other ts files.
I can fix this by adding something like import type { Custom } from 'other-module.js' in the generated file, but it would be much better to have this feature built-in.

@Wundero
Copy link

Wundero commented Jul 16, 2023

As an addendum to this, I have a use case where I want to put export type Custom = ColumnType<...>; into the file, and use the custom type as a column type. I think having some form of prelude where we can provide custom code which should be inserted into the generated file would be nice.

@valtyr
Copy link
Owner

valtyr commented Aug 21, 2023

I think an optional prelude is a good idea. I'll try to get to this soon.

@alandotcom
Copy link
Contributor

Workaround for now. We did this to have Json types be equivalent between Prisma and Kysely

declare global {
  export type JsonObject = { [Key in string]?: JsonValue }

  /**
   * From https://github.com/sindresorhus/type-fest/
   * Matches a JSON array.
   */
  export interface JsonArray extends Array<JsonValue> {}

  /**
   * From https://github.com/sindresorhus/type-fest/
   * Matches any valid JSON value.
   */
  export type JsonValue =
    | string
    | number
    | boolean
    | JsonObject
    | JsonArray
    | null
}

Can then be used in schema annotations, as the type is available globally

  /// @kyselyType(JsonValue)

To keep things clean, also possible to export in a global namespace,

declare global {
  namespace App {
      ....
  }
}

@valtyr
Copy link
Owner

valtyr commented May 8, 2024

Turns out this was possible all along, and I didn't realize it. See this comment:
#83 (comment)

@valtyr valtyr closed this as completed May 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants