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

Setting Maximum Limit will create tuple instead of array #60

Open
mohammadalijf opened this issue Feb 27, 2025 · 0 comments · May be fixed by #61
Open

Setting Maximum Limit will create tuple instead of array #60

mohammadalijf opened this issue Feb 27, 2025 · 0 comments · May be fixed by #61

Comments

@mohammadalijf
Copy link

when we have content type that has a group, in advance settings we can add Set Maximum Limit for it.
By doing so it will create a tuple for that field instead of an Array.

for instance

Image

will result following code which is wrong, because there might be no button or 1 or 2 or at max 3 buttons. Where as this type suggests there will be always 3 buttons.

buttons?: [
  {
    /** Button text */
    button_text: string;
    /** External URL */
    button_url?: string;
    /** Open link in a new tab */
    open_link_in_new_tab?: boolean;
  },
  {
    /** Button text */
    button_text: string;
    /** External URL */
    button_url?: string;
    /** Open link in a new tab */
    open_link_in_new_tab?: boolean;
  },
  {
    /** Button text */
    button_text: string;
    /** External URL */
    button_url?: string;
    /** Open link in a new tab */
    open_link_in_new_tab?: boolean;
  }
];

one work around is to add a utility type that mimics the expected behaviour

type BuildTuple<T, N extends number, R extends T[] = []> =
  R['length'] extends N ? R : BuildTuple<T, N, [...R, T]>;

// Recursively produce a union of all prefixes of a tuple
type TuplePrefixes<T extends any[]> = 
  T extends [any, ...infer Rest] ? T | TuplePrefixes<Rest extends any[] ? Rest : []> : [];

// The utility type: union of tuples with 0 to N copies of T
type MaxTuple<T, N extends number> = TuplePrefixes<BuildTuple<T, N>>;

// then it would become like this
buttons?: MaxTuple<
    {
        /** Button text */
        button_text: string;
        /** External URL */
        button_url?: string;
        /** Open link in a new tab */
        open_link_in_new_tab?: boolean;
    }, 3>
Image
@mohammadalijf mohammadalijf linked a pull request Feb 28, 2025 that will close this issue
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

Successfully merging a pull request may close this issue.

1 participant