Skip to content

Commit

Permalink
add readonly combinator
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti committed Mar 3, 2017
1 parent e56f931 commit 3283f82
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

- **New Feature**
- add `partial` combinator
- add `readonly` combinator
- add `never` type
- **Breaking Changes**
- remove `undefined` as valid value for `maybe` combinator
Expand Down
14 changes: 14 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,20 @@ export function tuple<RTS extends Array<Any>>(types: RTS, name?: string): TupleT
)
}

export class ReadonlyType<RT extends Any> extends Type<Readonly<TypeOf<RT>>> {
constructor(name: string, validate: Validate<Readonly<TypeOf<RT>>>, public readonly type: RT) {
super(name, validate)
}
}

export function readonly<RT extends Any>(type: RT, name?: string): ReadonlyType<RT> {
return new ReadonlyType(
name || `Readonly<${getTypeName(type)}>`,
(v, c) => type.validate(v, c).map(x => Object.freeze(x)),
type
)
}

export {
nullType as null,
undefinedType as undefined,
Expand Down

0 comments on commit 3283f82

Please sign in to comment.