Skip to content

Commit

Permalink
Simplify the typing of ZodRecord
Browse files Browse the repository at this point in the history
The RecordType conditional type seems to exist for handling the
partialness of indexed objects. However that is handled by the tsconfig
option noUncheckedIndexedAccess in [email protected] and later.
  • Loading branch information
googol committed Aug 23, 2023
1 parent 1e23990 commit 6563e80
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 30 deletions.
4 changes: 2 additions & 2 deletions deno/lib/__tests__/record.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ test("type inference", () => {

util.assertEqual<
recordWithEnumKeys,
Partial<Record<"Tuna" | "Salmon", string>>
Record<"Tuna" | "Salmon", string>
>(true);

util.assertEqual<
recordWithLiteralKeys,
Partial<Record<"Tuna" | "Salmon", string>>
Record<"Tuna" | "Salmon", string>
>(true);
});

Expand Down
15 changes: 2 additions & 13 deletions deno/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3332,24 +3332,13 @@ export interface ZodRecordDef<
}

export type KeySchema = ZodType<string | number | symbol, any, any>;
export type RecordType<K extends string | number | symbol, V> = [
string
] extends [K]
? Record<K, V>
: [number] extends [K]
? Record<K, V>
: [symbol] extends [K]
? Record<K, V>
: [BRAND<string | number | symbol>] extends [K]
? Record<K, V>
: Partial<Record<K, V>>;
export class ZodRecord<
Key extends KeySchema = ZodString,
Value extends ZodTypeAny = ZodTypeAny
> extends ZodType<
RecordType<Key["_output"], Value["_output"]>,
Record<Key["_output"], Value["_output"]>,
ZodRecordDef<Key, Value>,
RecordType<Key["_input"], Value["_input"]>
Record<Key["_input"], Value["_input"]>
> {
get keySchema() {
return this._def.keyType;
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/record.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ test("type inference", () => {

util.assertEqual<
recordWithEnumKeys,
Partial<Record<"Tuna" | "Salmon", string>>
Record<"Tuna" | "Salmon", string>
>(true);

util.assertEqual<
recordWithLiteralKeys,
Partial<Record<"Tuna" | "Salmon", string>>
Record<"Tuna" | "Salmon", string>
>(true);
});

Expand Down
15 changes: 2 additions & 13 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3332,24 +3332,13 @@ export interface ZodRecordDef<
}

export type KeySchema = ZodType<string | number | symbol, any, any>;
export type RecordType<K extends string | number | symbol, V> = [
string
] extends [K]
? Record<K, V>
: [number] extends [K]
? Record<K, V>
: [symbol] extends [K]
? Record<K, V>
: [BRAND<string | number | symbol>] extends [K]
? Record<K, V>
: Partial<Record<K, V>>;
export class ZodRecord<
Key extends KeySchema = ZodString,
Value extends ZodTypeAny = ZodTypeAny
> extends ZodType<
RecordType<Key["_output"], Value["_output"]>,
Record<Key["_output"], Value["_output"]>,
ZodRecordDef<Key, Value>,
RecordType<Key["_input"], Value["_input"]>
Record<Key["_input"], Value["_input"]>
> {
get keySchema() {
return this._def.keyType;
Expand Down

0 comments on commit 6563e80

Please sign in to comment.