细化集合检查器
array()
将值验证为 $ReadOnlyArray<T>
,给定类型为 Checker<T>
的值检查器。
// define checker
const check = array(number());
// test a value
const result = check([1,2,3]);
assert(result.type === 'success');
// result should typecheck
const value: $ReadOnlyArray<number> = result.value;
// test an invalid value
assert(check('test').type === 'failure');
assert(check(['test', 'other']).type === 'failure');
tuple()
将值验证为严格类型的元组。
// define checker
const check = tuple(number(), string(), bool());
// test a value
const result = check([1,'2', false]);
assert(result.type === 'success');
// result should typecheck
const value: $ReadOnly<[number, string, boolean]> = result.value;
// test an invalid value
assert(check('test').type === 'failure');
assert(check(['test', 'other']).type === 'failure');
dict()
将值验证为 $ReadOnly<{[key: string]: T}>
,给定类型为 Checker<T>
的值检查器。
// define checker
const check = dict(number());
// test a value
const result = check({a: 1, b: 2, c: 3});
assert(result.type === 'success');
// result should typecheck
const value: $ReadOnly<{[key: string]: number}> = result.value;
// test an invalid value
assert(check('test').type === 'failure');
assert(check({a: 'test', b: 'other', c: 3}).type === 'failure');
object()
将值验证为 $ReadOnly<{[key: K]: T}>
,给定类型为 {[key: K]: Checker<T>}
的检查器对象。
// define checker
const check = object({
a: number(),
b: string(),
c: optional(string()) // use `optional` for optional properties
});
// test a value
const result = check({a: 1, b: 'test'});
assert(result.type === 'success');
// result should typecheck
const value: $ReadOnly<{a: number, b: string}> = result.value;
// test an invalid value
assert(check('test').type === 'failure');
assert(check({a: 'test', b: 1}).type === 'failure');
assert(check({a: 1, c: 'test'}).type === 'failure');
set()
检查器用于断言混合值是否为具有有效值的 Set
类型。
// define checker
const check = set(number());
// test a value
const result = check(new Set([1, 2]));
assert(result.type === 'success');
// result should typecheck
const value: $ReadOnlySet<number> = result.value;
// test an invalid value
assert(check('test').type === 'failure');
assert(check({a: 'test', b: 'other', c: 3}).type === 'failure');
map()
检查器用于断言混合值是否为具有有效键和值的 Map
类型。
// define checker
const check = map(date(), number());
// test a value
const result = check(new Map([[new Date(), 1], [new Date(2), 2]]));
assert(result.type === 'success');
// result should typecheck
const value: $ReadOnlyMap<Date, number> = result.value;
// test an invalid value
assert(check('test').type === 'failure');
assert(check({a: 'test', b: 'other', c: 3}).type === 'failure');
writableArray()
与 array()
相同,但返回的类型是 Array<>
而不是 $ReadOnlyArray<>
const coerce = writableArray(number());
const result = coerce([1, 2, 3]);
assert(result.type === 'success', 'should succeed');
result.value[0] = 3;
writableObject()
与 object
相同,但返回的类型是可写对象而不是 $ReadOnly<>
const coerce = writableObject({
name: string(),
job: object({
years: number(),
title: string(),
}),
});
const result = coerce({name: 'Elsa', job: {title: 'Engineer', years: 3}});
assert(result.type === 'success', 'should succeed');
// should Flow check as writable
result.value.name = 'MechaElsa';
writableDict()
与 dict()
相同,但返回的类型是可写对象而不是 $ReadOnly<>
const coerce = writableDict(number());
const result = coerce({a: 1, b: 2});
assert(result.type === 'success', 'should succeed');
// should Flow check as writable
result.value.a = 3;