update type checking

This commit is contained in:
Nate Kelley 2025-07-17 10:05:50 -06:00
parent 5c15664b22
commit 54feda404a
No known key found for this signature in database
GPG Key ID: FD90372AB8D98B4F
2 changed files with 26 additions and 6 deletions

View File

@ -1,8 +1,12 @@
import type { organizations } from '@buster/database';
import { z } from 'zod';
import type { IsEqual } from '../type-utilities';
import { OrganizationRoleSchema } from './roles.types';
export const OrganizationColorPaletteSchema = z.object({
id: z.string(),
color: z.array(z.string()),
});
export const OrganizationSchema = z.object({
id: z.string(),
name: z.string(),
@ -14,8 +18,11 @@ export const OrganizationSchema = z.object({
domains: z.array(z.string()).nullable(),
restrictNewUserInvitations: z.boolean(),
defaultRole: OrganizationRoleSchema,
organizationColorPalettes: z.array(OrganizationColorPaletteSchema),
});
export type Organization = z.infer<typeof OrganizationSchema>;
type _DBEqualityCheck = IsEqual<Organization, typeof organizations.$inferSelect>;
// Type equality check - this will cause a compilation error if types don't match
const _organizationTypeCheck: Organization = {} as typeof organizations.$inferSelect;
const _databaseTypeCheck: typeof organizations.$inferSelect = {} as Organization;

View File

@ -2,9 +2,22 @@
IsEqual is a type utility that checks if two types are equal.
It's used to ensure that a database type is equal to a type. Like when we have a type that is a database type and we want to ensure that it's equal to the schema we have.
Example:
type _DBEqualityCheck = IsEqual<Organization, typeof organizations.$inferSelect>; // This will cause a compile error if Organization and organizations.$inferSelect don't match.
RECOMMENDED USAGE (more reliable):
Instead of using IsEqual<T, U>, use variable assignments for better error messages:
This will cause a compile error if Organization and OrganizationDB don't match.
// Type equality check - this will cause a compilation error if types don't match
const _organizationTypeCheck: Organization = {} as typeof organizations.$inferSelect;
const _databaseTypeCheck: typeof organizations.$inferSelect = {} as Organization;
This approach provides:
- Clearer error messages
- Better TypeScript error reporting
- More reliable type checking
*/
export type IsEqual<T, U> = [T] extends [U] ? ([U] extends [T] ? true : false) : false;
// Legacy type equality checker (variable assignment approach is recommended)
type StrictEqual<T, U> = (<G>() => G extends T ? 1 : 2) extends <G>() => G extends U ? 1 : 2
? true
: never;
export type IsEqual<T, U> = StrictEqual<T, U>;