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

feat(schema): added strong optional typing #3474

Merged
merged 1 commit into from
Jan 6, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 110 additions & 11 deletions packages/zero-schema/src/column.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,142 @@
export function string<T extends string = string>(optional: boolean = false) {
export function string<T extends string = string>(): {
type: 'string';
optional: false;
customType: T;
};
export function string<T extends string = string>(
optional: false,
): {
type: 'string';
optional: false;
customType: T;
};
export function string<T extends string = string>(
optional: true,
): {
type: 'string';
optional: true;
customType: T;
};
export function string<T extends string = string>(optional?: boolean) {
return {
type: 'string',
optional,
optional: optional ?? false,
customType: null as unknown as T,
} as const;
}

export function number<T extends number = number>(optional: boolean = false) {
export function number<T extends number = number>(): {
type: 'number';
optional: false;
customType: T;
};
export function number<T extends number = number>(
optional: false,
): {
type: 'number';
optional: false;
customType: T;
};
export function number<T extends number = number>(
optional: true,
): {
type: 'number';
optional: true;
customType: T;
};
export function number<T extends number = number>(optional?: boolean) {
return {
type: 'number',
optional,
optional: optional ?? false,
customType: null as unknown as T,
} as const;
}

export function boolean<T extends boolean = boolean>(): {
type: 'boolean';
optional: false;
customType: T;
};
export function boolean<T extends boolean = boolean>(
optional: boolean = false,
) {
optional: false,
): {
type: 'boolean';
optional: false;
customType: T;
};
export function boolean<T extends boolean = boolean>(
optional: true,
): {
type: 'boolean';
optional: true;
customType: T;
};
export function boolean<T extends boolean = boolean>(optional?: boolean) {
return {
type: 'boolean',
optional,
optional: optional ?? false,
customType: null as unknown as T,
} as const;
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function json<T = any>(optional: boolean = false) {
export function json<T = any>(): {
type: 'json';
optional: false;
customType: T;
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function json<T = any>(
optional: false,
): {
type: 'json';
optional: false;
customType: T;
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function json<T = any>(
optional: true,
): {
type: 'json';
optional: true;
customType: T;
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function json<T = any>(optional?: boolean) {
return {
type: 'json',
optional,
optional: optional ?? false,
customType: null as unknown as T,
} as const;
}

export function enumeration<T extends string>(optional: boolean = false) {
export function enumeration<T extends string>(): {
type: 'string';
kind: 'enum';
optional: false;
customType: T;
};
export function enumeration<T extends string>(
optional: false,
): {
type: 'string';
kind: 'enum';
optional: false;
customType: T;
};
export function enumeration<T extends string>(
optional: true,
): {
type: 'string';
kind: 'enum';
optional: true;
customType: T;
};
export function enumeration<T extends string>(optional?: boolean) {
return {
type: 'string',
kind: 'enum',
customType: null as unknown as T,
optional,
optional: optional ?? false,
} as const;
}
Loading