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/06f0362221f4fe392c208bc59a49fd9e to your computer and use it in GitHub Desktop.
Save baptistemanson/06f0362221f4fe392c208bc59a49fd9e to your computer and use it in GitHub Desktop.
// SOA
import { AppAccount, Collaborator } from "@prisma/client";
// list of all row type in the app
enum TableRowTypeString {
Collaborator,
AppAccount,
}
// list of all fields for app accounts
enum AppAccountPaths {
role = "role",
status = "status",
}
// list of all fields for collaborator
enum CollaboratorPaths {
hello = "hello",
world = "world",
}
// list of all fields for all row types
type Fields = {
[TableRowTypeString.Collaborator]: CollaboratorPaths;
[TableRowTypeString.AppAccount]: AppAccountPaths;
};
type TData = {
[TableRowTypeString.Collaborator]: Partial<Collaborator>;
[TableRowTypeString.AppAccount]: Partial<AppAccount>;
};
// example of a type that forces a given enum based on the TableRowTypeString
type Column<T extends TableRowTypeString> = {
requiredFields: Fields[T];
data: TData[T];
};
function getTextColumnDef<T extends TableRowTypeString>(
props: Partial<Column<T>> & { requiredFields: Fields[T] },
): Column<T> {
return { ...props, data: { id: "a" } };
}
const prout: Column<TableRowTypeString.AppAccount> = 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