Skip to content

Commit c9b7b36

Browse files
committed
feat: add readonly/readonlyArray
1 parent 35b1a54 commit c9b7b36

File tree

5 files changed

+52
-0
lines changed

5 files changed

+52
-0
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,11 @@ A validator that requires the value to be `string | null | undefined`.
313313

314314
A 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

318323
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
354359
PersonType.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

359368
Merges the properties of multiple object validators together into an exact object validator (no additional properties are allowed).

src/index.js.flow

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ declare export function any(): Type<any>
6868
declare export function unknown(): Type<mixed>
6969

7070
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>>
7176

7277
declare export function nullLiteral(): Type<null>
7378
export { nullLiteral as null }

src/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ export const unknown = (): Type<unknown> => new UnknownType()
6868
export 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+
7177
export const nullLiteral = (): Type<null> => new NullLiteralType()
7278
export { nullLiteral as null }
7379
export const nullOr = <T>(type: Type<T>): Type<T | null> =>

test/readonly.spec.js.flow

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
})

test/readonlyArray.spec.js.flow

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
})

0 commit comments

Comments
 (0)