electrodb icon indicating copy to clipboard operation
electrodb copied to clipboard

Enum Attributes are not typed properly (any) in Attribute Getters and Setters

Open boringContributor opened this issue 7 months ago • 0 comments

Describe the bug

If I use an enum like attribute (["public", "private"] as const) then the get or set property used to watch other attribute changes are of type any.

visibility: { 
			type: ["public", "private"] as const,
			watch: ['public'], // watch public to update visibility automatically
			get(attr: any, item: any) {
				if(attr) return attr; // use the visiblity when set
				
				// fallback
				if (item?.public === true) return "public";
				if (item?.public === false) return "private";

				return "private"; // default to private if not set
			},
		 },

ElectroDB Version 3.4.3

ElectroDB Playground Link ElectroDB Playground

Entity/Service Definitions Include your entity model (or a model that sufficiently recreates your issue) to help troubleshoot.

const tasks = new Entity(
  {
    model: {
      entity: "tasks",
      version: "1",
      service: "taskapp"
    },
    attributes: {
      visibility: {
        watch: ["public"],
        get: (value, item) => {
          // value and item is of type any
        },
        type: ["private", "public"] as const,
      },
      public: {
        type: "boolean"
      }
    },
    indexes: {
      projects: {
        pk: {
          field: "pk",
          composite: ["team"]
        },
        sk: {
          field: "sk",
          // create composite keys for partial sort key queries
          composite: ["project", "task"]
        }
      },
      assigned: {
        // collections allow for queries across multiple entities
        collection: "assignments",
        index: "gsi1pk-gsi1sk-index",
        pk: {
          // map to your GSI Hash/Partition key
          field: "gsi1pk",
          composite: ["user"]
        },
        sk: {
          // map to your GSI Range/Sort key
          field: "gsi1sk",
          composite: ["status"]
        }
      },
      backlog: {
        // map to the GSI name on your DynamoDB table
        index: "gsi2pk-gsi2sk-index",
        pk: {
          field: "gsi2pk",
          composite: ["project"]
        },
        sk: {
          field: "gsi2sk",
          composite: ["team", "closed"],
        }
      }
    }
  },
  { table }
);

Expected behavior The data being passed to get and set should be typed when the attribute type is annotated with as const e.g. an enum

Additional context If this is not seen as a bug ticket then sorry 🙏🏼

boringContributor avatar Jul 15 '25 10:07 boringContributor