本文整理汇总了TypeScript中tcomb.interface函数的典型用法代码示例。如果您正苦于以下问题:TypeScript interface函数的具体用法?TypeScript interface怎么用?TypeScript interface使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了interface函数的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: describe
describe('connect', () => {
type ST = { view: string };
const { connect } = stateInit(t.interface<ST>({ view: t.String }, { strict: true }));
it('should throw if select is invalid', () => {
expect(() => connect(['bar'])).toThrowError('bar is not defined in state');
});
});
示例2: describe
describe('transition', () => {
type ST = { view: string };
const stateType = t.interface<ST>({ view: t.String }, { strict: true });
it('should do nothing if there is no state diff', () => {
const { transition, states } = prepareTransition({ stateType, initialState: { view: 'bar' } });
transition({ view: 'bar' });
expect(states).toEqual([{ view: 'bar' }]);
});
it('should push synchronously if there is state diff', () => {
const { transition, states } = prepareTransition({ stateType, initialState: { view: 'bar' } });
transition({ view: 'baz' });
expect(states).toEqual([{ view: 'bar' }, { view: 'baz' }]);
});
it('should throw if it evaluates to a state with a key of invalid type', () => {
const { transition } = prepareTransition({ stateType, initialState: { view: 'bar' } });
const invalidTransition = () => {
transition({ view: 2 });
};
expect(invalidTransition).toThrow();
});
it('should throw if it evaluates to a state with an extraneous key', () => {
const { transition } = prepareTransition({ stateType, initialState: { view: 'bar' } });
const invalidTransition = () => {
transition({ view: 'bar', bar: 'foo' });
};
expect(invalidTransition).toThrow();
});
});
示例3: it
import { parse as _parse, stringify as _stringify } from '../src/parser';
import * as t from 'tcomb';
type S = {
view: string;
numb: number;
bool: boolean;
optNumb?: number;
optBool?: boolean;
};
const ST = t.interface<S>({
view: t.String,
numb: t.Number,
bool: t.Boolean,
optNumb: t.maybe(t.Number),
optBool: t.maybe(t.Boolean)
});
const parse = _parse(ST);
const stringify = _stringify(ST);
describe('parse', () => {
it('should parse numbers', () => {
expect(parse({ view: 'foo', numb: '4' })).toMatchSnapshot();
});
it('should parse optional numbers', () => {
expect(parse({ view: 'foo', optNumb: '4' })).toMatchSnapshot();
});
it('should parse booleans', () => {
expect(parse({ view: 'foo', bool: 'true' })).toMatchSnapshot();
});
it('should parse optional booleans', () => {
示例4: it
it('should throw if initialState is invalid', () => {
type StateType = { view: string };
const stateType = t.interface<StateType>({ view: t.String }, { name: 'AppState', strict: true });
const { run } = stateInit(stateType);
expect(() => run({ initialState: { foo: 1 } })).toThrow();
});
示例5: expect
expect(() => stateInit(t.interface({}, { strict: false }))).toThrowError(