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 safety for inputs const #1

Open
cbejensen opened this issue Mar 31, 2023 · 1 comment
Open

Type safety for inputs const #1

cbejensen opened this issue Mar 31, 2023 · 1 comment

Comments

@cbejensen
Copy link

Since we have to cast our inputs variable as const, there's nothing to keep us from mistyping an input. Seems like we're basically trading type safety for the Builder inputs with type safety for our custom components. It would be nice if this lib exported a helper to have the best of both worlds.

Here's one way that seems to work:

import type { Input } from '@builder.io/sdk';

// Readonly only goes one level deep, so we need our own utility type
type DeepReadonly<T> =
    T extends (infer R)[] ? DeepReadonlyArray<R> :
    T extends Function ? T :
    T extends object ? DeepReadonlyObject<T> :
    T;

interface DeepReadonlyArray<T> extends ReadonlyArray<DeepReadonly<T>> {}

type DeepReadonlyObject<T> = {
    readonly [P in keyof T]: DeepReadonly<T[P]>;
};

const inputs = [
  {
    name: 'tabs',
    type: 'list',
    subFields: [
      {
        name: 'label',
        type: 'text',
        defaultValue: 'New tab',
      },
      {
        name: 'content',
        type: 'uiBlocks',
        defaultValue: [],
      },
    ],
    defaultValue: [
      {
        label: 'Tab 1',
        content: [],
      },
    ],
  },
] as const satisfies DeepReadonly<Input[]>;

Maybe we could export type BuilderInputs = DeepReadOnly<Input[]> so users can just do as const satisfies BuilderInputs?

@davedbase
Copy link
Owner

Hmmm, I would definitely like to explore this. Would you mind submitting a PR? I'll give it a spin and released it under a next tag then get some feedback from a couple projects I know that are using this utility. :)

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

2 participants