sam-goodwin / eventual

Build scalable and durable micro-services with APIs, Messaging and Workflows
https://docs.eventual.ai
MIT License
174 stars 4 forks source link

Entity types too strict #408

Closed sam-goodwin closed 1 year ago

sam-goodwin commented 1 year ago

This doesn't work:

export interface StringType {
  type: string;
}

const foo = entity("Foo", {
  partition: ["pk"],
  attributes: {
    pk: z.string(),
    broken: null as any as z.ZodType<StringType>
  }
});
Type 'JSONSchemaType' is not assignable to type 'ZodType<AttributeValue, ZodTypeDef, AttributeValue>'.
  Types of property '_type' are incompatible.
    Type 'StringType' is not assignable to type 'AttributeValue'.
      Type 'StringType' is not assignable to type 'Attributes'.
        Index signature for type 'string' is missing in type 'StringType'.ts(2322)

But this does:

broken: null as any as z.ZodType<{type: string}>
thantos commented 1 year ago

The type error is by design in typescript. interfaces without a index signature cannot be used as a type that does (ex: Attribute). And the Attribute/AttributeValue type is trying to limit property types to match what dynamo can handle.

Plus the code sample here is a hack that would break if said field was used as a key or we ever generate jsonschema/openai for entities.

Options:

  1. close as by design
  2. change AttributeValue to any and do not type check the definition of the entity attributes.