Skip to content

Commit 449779a

Browse files
committed
feat: add readonly flags to schemas
Previously, TypeScript allowed you to modify schema objects at the typing level, but this modification caused errors because the schema objects themselves were frozen at the JavaScript level. This commit adds readonly flags for schemas to fix the problem above.
1 parent f8758fd commit 449779a

2 files changed

Lines changed: 7 additions & 5 deletions

File tree

types/property.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ export type PropertySchema<
8888
E extends PropertyElementType = any,
8989
R extends PropertyRequired = any,
9090
D extends PropertyDefault<T, E> = any
91-
> = Required<PropertySchemaTemplate<T, E, R, D>>
91+
> = Readonly<Required<PropertySchemaTemplate<T, E, R, D>>>
9292

9393
/**
9494
* For creating the `PropertySchema` object you need to pass some options

types/schema.d.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,16 @@ import {
4444
* @public
4545
*/
4646
export type Schema<SRaw extends RawSchema = any> =
47-
SRaw extends RawSchemaAsObject ? SchemaAsObject<SRaw> : SchemaAsAnyObject
47+
SRaw extends RawSchemaAsObject
48+
? Readonly<SchemaAsObject<SRaw>>
49+
: Readonly<SchemaAsAnyObject>
4850

4951
type SchemaAsObject<SRaw extends RawSchemaAsObject> = {
50-
[P in keyof SRaw]: SchemaTypeProperty<SRaw[P]>
52+
readonly [P in keyof SRaw]: SchemaTypeProperty<SRaw[P]>
5153
}
5254

5355
type SchemaAsAnyObject = {
54-
[key: string]: Schema | PropertySchema
56+
readonly [key: string]: Schema | PropertySchema
5557
}
5658

5759
type SchemaTypeProperty<P> = P extends PropertyTypeRaw
@@ -132,7 +134,7 @@ export type RawSchemaProperty =
132134
* @public
133135
*/
134136
export type SchemaReturn<S extends Schema = any> = {
135-
[P in keyof S]: SchemaReturnProperty<S[P]>
137+
-readonly [P in keyof S]: SchemaReturnProperty<S[P]>
136138
}
137139

138140
type SchemaReturnProperty<P extends Schema | PropertySchema | PropertyTypeRaw> =

0 commit comments

Comments
 (0)