mirror of https://github.com/buster-so/buster.git
update type checking
This commit is contained in:
parent
5c15664b22
commit
54feda404a
|
@ -1,8 +1,12 @@
|
||||||
import type { organizations } from '@buster/database';
|
import type { organizations } from '@buster/database';
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
import type { IsEqual } from '../type-utilities';
|
|
||||||
import { OrganizationRoleSchema } from './roles.types';
|
import { OrganizationRoleSchema } from './roles.types';
|
||||||
|
|
||||||
|
export const OrganizationColorPaletteSchema = z.object({
|
||||||
|
id: z.string(),
|
||||||
|
color: z.array(z.string()),
|
||||||
|
});
|
||||||
|
|
||||||
export const OrganizationSchema = z.object({
|
export const OrganizationSchema = z.object({
|
||||||
id: z.string(),
|
id: z.string(),
|
||||||
name: z.string(),
|
name: z.string(),
|
||||||
|
@ -14,8 +18,11 @@ export const OrganizationSchema = z.object({
|
||||||
domains: z.array(z.string()).nullable(),
|
domains: z.array(z.string()).nullable(),
|
||||||
restrictNewUserInvitations: z.boolean(),
|
restrictNewUserInvitations: z.boolean(),
|
||||||
defaultRole: OrganizationRoleSchema,
|
defaultRole: OrganizationRoleSchema,
|
||||||
|
organizationColorPalettes: z.array(OrganizationColorPaletteSchema),
|
||||||
});
|
});
|
||||||
|
|
||||||
export type Organization = z.infer<typeof OrganizationSchema>;
|
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;
|
||||||
|
|
|
@ -2,9 +2,22 @@
|
||||||
IsEqual is a type utility that checks if two types are equal.
|
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.
|
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:
|
RECOMMENDED USAGE (more reliable):
|
||||||
type _DBEqualityCheck = IsEqual<Organization, typeof organizations.$inferSelect>; // This will cause a compile error if Organization and organizations.$inferSelect don't match.
|
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>;
|
||||||
|
|
Loading…
Reference in New Issue