vinejs / vine

VineJS is a form data validation library for Node.js
https://vinejs.dev
MIT License
1.1k stars 21 forks source link

chore: export ObjectGroup and GroupConditional #55

Closed Melchyore closed 5 months ago

Melchyore commented 5 months ago

❓ Type of change

📚 Description

Hey!

I'm working on a package to generate OpenAPI specs automatically based on the AST of AdonisJS apps. To do so, I need to augment the types of some VineJS methods (which is the case of the merge method of VineObject schema). But I couldn't do it, since GroupObject and GroupConditional are not exported to the userland.

This is the current type of the merge method:

merge<Group extends ObjectGroup<GroupConditional<any, any, any>>>(
    group: Group
  ): VineObject<
    Properties,
    Output & Group[typeof OTYPE],
    CamelCaseOutput & Group[typeof COTYPE]
  >
}

And this is what I'm trying to achieve:

merge<Group extends ObjectGroup<GroupConditional<any, any, any>>>(
    group: Group
  ): VineObject<
    Properties,
    Output & Group[typeof symbols.OTYPE],
    CamelCaseOutput & Group[typeof symbols.COTYPE]
  > &
    Group
}

As you can see, I need the two classes to access to Group type.

Is it possible to accept this PR, please?

Julien-R44 commented 5 months ago

Would something like that could work for you instead?

declare module '@vinejs/vine' {
  type Group = ReturnType<typeof vine.group>;

  interface MyModuleAugmentation {
     foo(): string & Group
  }
}

But maybe exporting these classes is ok. Naively I'm thinking that it would be exposing internals but maybe I'm wrong. @thetutlage will confirm

Melchyore commented 5 months ago

Yup, it works, but with a slightly different version. The type must be declared outside of the declare module block.

type ObjectGroup = ReturnType<typeof vine.group>

declare module '@vinejs/vine' {
  interface VineObject<Properties extends Record<string, SchemaTypes>, Output, CamelCaseOutput>
    extends BaseType<Output, CamelCaseOutput> {
    merge<Group extends ObjectGroup>(
      group: Group
    ): VineObject<
      Properties,
      Output & Group[typeof symbols.OTYPE],
      CamelCaseOutput & Group[typeof symbols.COTYPE]
    > &
      Group
  }
}

Thank you!