-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4407abd
commit 3bda3ec
Showing
6 changed files
with
72 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// @flow | ||
|
||
import Type from './Type' | ||
|
||
declare class OpaqueType<T> extends Type<T> { | ||
type: () => Type<any>; | ||
|
||
constructor(type: () => Type<any>): void; | ||
} | ||
|
||
export default OpaqueType |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import Type from './Type' | ||
import Validation, { IdentifierPath } from '../Validation' | ||
import RuntimeTypeErrorItem from '../errorReporting/RuntimeTypeErrorItem' | ||
|
||
export default class TypeReference<T> extends Type<T> { | ||
typeName = 'TypeReference' | ||
readonly type: () => Type<any> | ||
|
||
constructor(type: () => Type<any>) { | ||
super() | ||
this.type = type | ||
} | ||
|
||
resolveType(): Type<any> { | ||
return this.type().resolveType() | ||
} | ||
|
||
*errors( | ||
validation: Validation, | ||
path: IdentifierPath, | ||
input: any | ||
): Iterable<RuntimeTypeErrorItem> { | ||
yield* this.type().errors(validation, path, input) | ||
} | ||
|
||
accepts(input: any): input is T { | ||
return this.type().accepts(input) | ||
} | ||
|
||
get acceptsSomeCompositeTypes(): boolean { | ||
return this.type().acceptsSomeCompositeTypes | ||
} | ||
|
||
toString(): string { | ||
return this.type().toString() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import * as t from '../src' | ||
import { expect } from 'chai' | ||
|
||
describe(`t.opaque`, function() { | ||
const DateString: t.OpaqueType<any> = t.opaque(() => t.string()) | ||
|
||
it(`works`, function() { | ||
DateString.assert('foo') | ||
expect(DateString.accepts('foo')).to.be.true | ||
}) | ||
}) |