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`.
313313
314314A validator that requires the value to be ` number[] ` .
315315
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+
316321### ` t.object(properties) `
317322
318323A 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
354359PersonType .assert ({ name: ' dude' , age: ' old' }) // error
355360```
356361
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+
357366### ` t.merge(...objectTypes) `
358367
359368Merges 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>
6868declare export function unknown(): Type<mixed>
6969
7070declare 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>>
7176
7277declare export function nullLiteral(): Type<null>
7378export { nullLiteral as null }
Original file line number Diff line number Diff line change @@ -68,6 +68,12 @@ export const unknown = (): Type<unknown> => new UnknownType()
6868export const array = < T > ( elementType : Type < T > ) : Type < T [ ] > =>
6969 new ArrayType ( elementType )
7070
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+
7177export const nullLiteral = ( ) : Type < null > => new NullLiteralType ( )
7278export { nullLiteral as null }
7379export 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