File tree Expand file tree Collapse file tree 5 files changed +52
-0
lines changed Expand file tree Collapse file tree 5 files changed +52
-0
lines changed Original file line number Diff line number Diff line change @@ -313,6 +313,11 @@ A validator that requires the value to be `string | null | undefined`.
313
313
314
314
A validator that requires the value to be ` number[] ` .
315
315
316
+ ### ` t.readonlyArray(t.number()) `
317
+
318
+ A validator that requires the value to be ` number[] ` .
319
+ Doesn't require the value to be frozen; just allows the extracted type to be ` ReadonlyArray ` .
320
+
316
321
### ` t.object(properties) `
317
322
318
323
A validator that requires the value to be an object with all of the given required properties an no additional properties.
@@ -354,6 +359,10 @@ PersonType.assert({ name: 1 }) // error
354
359
PersonType .assert ({ name: ' dude' , age: ' old' }) // error
355
360
```
356
361
362
+ ### ` t.readonly(objectType) `
363
+
364
+ Use ` t.readOnly(t.object(...)) ` or ` t.readOnly(t.merge(...)) ` etc. Doesn't require the object to be frozen, just allows the extracted type to be readonly.
365
+
357
366
### ` t.merge(...objectTypes) `
358
367
359
368
Merges the properties of multiple object validators together into an exact object validator (no additional properties are allowed).
Original file line number Diff line number Diff line change @@ -68,6 +68,11 @@ declare export function any(): Type<any>
68
68
declare export function unknown(): Type<mixed>
69
69
70
70
declare export function array<T>(elementType: Type<T>): Type<T[]>
71
+ declare export function readonlyArray<T>(
72
+ elementType: Type<T>
73
+ ): Type<$ReadOnlyArray<T>>
74
+
75
+ declare export function readonly<T: {}>(type: Type<T>): Type<$ReadOnly<T>>
71
76
72
77
declare export function nullLiteral(): Type<null>
73
78
export { nullLiteral as null }
Original file line number Diff line number Diff line change @@ -68,6 +68,12 @@ export const unknown = (): Type<unknown> => new UnknownType()
68
68
export const array = < T > ( elementType : Type < T > ) : Type < T [ ] > =>
69
69
new ArrayType ( elementType )
70
70
71
+ export const readonlyArray = < T > ( elementType : Type < T > ) : Type < readonly T [ ] > =>
72
+ new ArrayType ( elementType ) as any
73
+
74
+ export const readonly = < T extends { } > ( type : Type < T > ) : Type < Readonly < T > > =>
75
+ type as any
76
+
71
77
export const nullLiteral = ( ) : Type < null > => new NullLiteralType ( )
72
78
export { nullLiteral as null }
73
79
export const nullOr = < T > ( type : Type < T > ) : Type < T | null > =>
Original file line number Diff line number Diff line change
1
+ // @flow
2
+
3
+ /* eslint-disable @typescript-eslint/no-unused-vars */
4
+
5
+ import * as t from '../src/index'
6
+ import { describe, it } from 'mocha'
7
+
8
+ describe('readonly', () => {
9
+ type Person = $ReadOnly<{|
10
+ name: string,
11
+ |}>
12
+
13
+ const PersonType: t.TypeAlias<Person> = t.alias(
14
+ 'Person',
15
+ t.readonly(t.object({ name: t.string() }))
16
+ )
17
+ })
Original file line number Diff line number Diff line change
1
+ // @flow
2
+
3
+ /* eslint-disable @typescript-eslint/no-unused-vars */
4
+
5
+ import * as t from '../src/index'
6
+ import { describe, it } from 'mocha'
7
+
8
+ describe('readonlyArray', () => {
9
+ type Nums = $ReadOnlyArray<number>
10
+
11
+ const PersonType: t.TypeAlias<Nums> = t.alias(
12
+ 'Person',
13
+ t.readonlyArray(t.number())
14
+ )
15
+ })
You can’t perform that action at this time.
0 commit comments