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

Improve schema type using const type parameters #335

Open
thorhj opened this issue Dec 12, 2023 · 0 comments
Open

Improve schema type using const type parameters #335

thorhj opened this issue Dec 12, 2023 · 0 comments

Comments

@thorhj
Copy link

thorhj commented Dec 12, 2023

The entity schema is supposed to be unchangeable. Its purpose is to provide information for queries and typings.

I noticed this info box on the collections page:

TypeScript Note: Use as const syntax when defining collection as a string array for improved type support

This relates to sub-collections defined in the array syntax:

indexes: {
    projects: {
      collection: ["contributions", "assignments"] as const, // <--------------------------- HERE
      index: "gsi2",
      pk: {
        field: "gsi2pk",
        composite: ["employeeId"],
      },
      sk: {
        field: "gsi2sk",
        composite: [],
      },
    },
  },

I think this, and probably other similar cases, can be avoided using the const type parameters feature from Typescript 5. In index.d.ts I think every occurence of Schema can be made const in this way. For instance:

export class Entity<
  A extends string,
  F extends string,
  C extends string,
  const S extends Schema<A, F, C>, // <--------------------------- HERE
> { ... }

Here is a small contained example:

type Schema<C extends string> = {
  collection?: AccessPatternCollection<C>;
};

type AccessPatternCollection<C extends string> = C | ReadonlyArray<C>;

class Entity<C extends string, const S extends Schema<C>> {
  private schema: S;
  public constructor(schema: S) {
    this.schema = schema;
  }
}

const MyEntity = new Entity({
  collection: ["foo", "bar"],
});
/*
  const MyEntity: Entity<string, {
      readonly collection: readonly ["foo", "bar"];
  }>
*/

As you can see, MyEntity.collection is typed exactly like if we had used as const Using const type parameters is better than as const since it lessens the burden on the library consumers.

I haven't tested this, but my assumption is that every occurrence of

S extends Schema<A, F, C>

can be replaced with

const S extends Schema<A, F, C>

Caveats:

  • The type tests in the project will need a big overhaul following this change.
  • To use this feature I think consumers are required to use Typescript 5.
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

1 participant