tywalch / electrodb

A DynamoDB library to ease the use of modeling complex hierarchical relationships and implementing a Single Table Design while keeping your query code readable.
MIT License
956 stars 58 forks source link

Typescript Warning when Omitting Non Required Attributes on Create/ Put #344

Closed fmvfelipemonsalve closed 3 months ago

fmvfelipemonsalve commented 5 months ago

Describe the bug When the sort key for the primary index of an entity is an empty array, I'm getting a Typescript error with the create method for the entity, when I don't include non-required fields in the object I pass to the method.

If I remove the sort key, or add an actual attribute to the sort key's composite field, the error disappears.

ElectroDB Version 2.13.0

ElectroDB Playground Link ElectroDB Playground Example.

The error doesn't show up on the playground.

Entity/Service Definitions

{
    model: {
      entity: "customer",
      service: "palomma",
      version: "1",
    },
    attributes: {
      id: {
        type: "string",
        required: true,
        readOnly: true,
        default: () => ulid(),
      },
      firstName: {
        type: "string",
        required: true,
      },
      lastName: {
        type: "string",
        required: true,
      },
      age: {
        type: "number",
        required: true,
      },
      email: {
        type: "string",
      },
      createdAt: {
        type: "string",
        readOnly: true,
        required: true,
        default: () => new Date().toISOString(),
      },
      updatedAt: {
        type: "string",
        watch: "*",
        required: true,
        default: () => new Date().toISOString(),
        set: (updatedAt) =>
          updatedAt && !isNaN(Date.parse(updatedAt))
            ? updatedAt
            : new Date().toISOString(),
      },
    },
    indexes: {
      primary: {
        pk: { field: "pk", composite: ["id"] },
        sk: { field: "sk", composite: [] },
      },
    },
  }

Expected behavior No Typescript warning should appear for omitting non-required fields.

Errors

Argument of type '{ firstName: string; lastName: string; age: number; }' is not assignable to parameter of type 'PutItem<string, string, string, { model: { entity: string; service: string; version: string; }; attributes: { id: { type: "string"; required: true; readOnly: true; default: () => string; }; firstName: { type: "string"; required: true; }; ... 4 more ...; updatedAt: { ...; }; }; indexes: { ...; }; }>'.
  Property 'email' is missing in type '{ firstName: string; lastName: string; age: number; }' but required in type 'Pick<CreatedItem<string, string, string, { model: { entity: string; service: string; version: string; }; attributes: { id: { type: "string"; required: true; readOnly: true; default: () => string; }; firstName: { type: "string"; required: true; }; ... 4 more ...; updatedAt: { ...; }; }; indexes: { ...; }; }, { ...; }...'.ts(2345)
test.ts(30, 7): 'email' is declared here.
tywalch commented 4 months ago

Hi @fmvfelipemonsalve 👋

Are you able to share the version of typescript you are using? The playground uses the latest version of typescript (making it a great place to compare local behavior against the latest versions of electrodb and typescript) and it doesn't seem to show the same error. Depending on the version you're using, it may not be compatible with some of the inference syntax used in the library. The good news is, it might be as simple as updating your version 👍

tywalch commented 3 months ago

Closing this ticket but if you're able to share the info above feel free to reopen!