Skip to content

Can we make trait names type safe? #865

@jackfranklin

Description

@jackfranklin

If you've defined traits on a builder, it would be awesome if when you called that builder we checked if the provided traits were defined.

So rather than traits being string[], it could calculate the list of available options.

The goal here would be that when I create:

const userBuilder = build<User>({
  fields: { isAdmin: false },
  traits: {
    admin: { overrides: { isAdmin: true } },
    someOtherThing: {...}
  },
});

I can then call it:

const user = userBuilder({ traits: ['admin'] });

But the type of traits in this call should now be ('admin'|'someOtherThing')[], because TS has figured out the keys of the traits I provided when I created the builder.

Currently, traits is typed as string[], so we don't get any type checks should you pass in a trait that doesn't exist.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions