Skip to content

Instantly share code, notes, and snippets.

@baptistemanson
Created April 12, 2024 09:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save baptistemanson/39f8471f173198576e9f3d8b03f5204e to your computer and use it in GitHub Desktop.
Save baptistemanson/39f8471f173198576e9f3d8b03f5204e to your computer and use it in GitHub Desktop.
// AOS
import { AppAccount, Collaborator } from "@prisma/client";
// list of all row type in the app
enum TableRowType {
Collab,
AppAcc,
}
type Spec<A, B> = {
fields: A;
data: Partial<B>;
};
// list of all fields for app accounts
enum AppAccountPaths {
role = "role",
status = "status",
}
type ColumnAppAccountSpec = Spec<AppAccountPaths, AppAccount>;
// list of all fields for collaborator
enum CollaboratorPaths {
hello = "hello",
world = "world",
}
type ColumnCollaboratorSpec = Spec<CollaboratorPaths, Collaborator>;
type ColumnDef = {
[TableRowType.Collab]: ColumnCollaboratorSpec;
[TableRowType.AppAcc]: ColumnAppAccountSpec;
};
// example of a type that forces a given enum based on the TableRowTypeString
type Column<T extends TableRowType> = {
requiredFields: ColumnDef[T]["fields"];
data: ColumnDef[T]["data"];
};
function getTextColumnDef<T extends TableRowType>(
props: Partial<Column<T>> & { requiredFields: ColumnDef[T]["fields"] },
): Column<T> {
return { ...props, data: { id: "rew" } };
}
const prout: Column<TableRowType.AppAcc> = getTextColumnDef({
requiredFields: AppAccountPaths.role,
});
console.log(prout);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment