本文整理汇总了TypeScript中io-ts.literal函数的典型用法代码示例。如果您正苦于以下问题:TypeScript literal函数的具体用法?TypeScript literal怎么用?TypeScript literal使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了literal函数的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: Error
export const validateConfigurationBlocks = (configurationBlocks: ConfigurationBlock[]) => {
const validationMap = {
isHosts: t.array(t.string),
isString: t.string,
isPeriod: t.string,
isPath: t.string,
isPaths: t.array(t.string),
isYaml: t.string,
};
for (const [index, block] of configurationBlocks.entries()) {
const blockSchema = configBlockSchemas.find(s => s.id === block.type);
if (!blockSchema) {
throw new Error(
`Invalid config type of ${block.type} used in 'configuration_blocks' at index ${index}`
);
}
const interfaceConfig = blockSchema.configs.reduce(
(props, config) => {
if (config.options) {
props[config.id] = t.union(config.options.map(opt => t.literal(opt.value)));
} else if (config.validation) {
props[config.id] = validationMap[config.validation];
}
return props;
},
{} as t.Props
);
const runtimeInterface = createConfigurationBlockInterface(
t.literal(blockSchema.id),
t.interface(interfaceConfig)
);
const validationResults = runtimeInterface.decode(block);
if (validationResults.isLeft()) {
throw new Error(
`configuration_blocks validation error, configuration_blocks at index ${index} is invalid. ${
PathReporter.report(validationResults)[0]
}`
);
}
}
};
示例2:
props[config.id] = t.union(config.options.map(opt => t.literal(opt.value)));
示例3:
['oss', 'trial', 'standard', 'basic', 'gold', 'platinum'].map(s => t.literal(s))
示例4:
configBlockSchemas.map(s => t.literal(s.id))
示例5:
export type Empty = iots.TypeOf<typeof Empty_IO>;
export const X_IO = iots.interface({
x: C5TCurrent.Int32_IO,
}, 'X');
export type X = iots.TypeOf<typeof X_IO>;
export const E_IO = C5TCurrent.Enum_IO('E');
export type E = iots.TypeOf<typeof E_IO>;
export const Y_IO = iots.interface({
e: E_IO,
}, 'Y');
export type Y = iots.TypeOf<typeof Y_IO>;
export const MyFreakingVariant_VariantCase_A_IO = iots.interface({ "A": A_IO, "": iots.union([ iots.undefined, iots.literal("T9206911749438269255") ]), "$": iots.union([ iots.undefined, iots.literal("T9206911749438269255") ]), });
export type MyFreakingVariant_VariantCase_A = iots.TypeOf<typeof MyFreakingVariant_VariantCase_A_IO>;
export const MyFreakingVariant_VariantCase_X_IO = iots.interface({ "X": X_IO, "": iots.union([ iots.undefined, iots.literal("T9209980946934124423") ]), "$": iots.union([ iots.undefined, iots.literal("T9209980946934124423") ]), });
export type MyFreakingVariant_VariantCase_X = iots.TypeOf<typeof MyFreakingVariant_VariantCase_X_IO>;
export const MyFreakingVariant_VariantCase_Y_IO = iots.interface({ "Y": Y_IO, "": iots.union([ iots.undefined, iots.literal("T9208828720332602574") ]), "$": iots.union([ iots.undefined, iots.literal("T9208828720332602574") ]), });
export type MyFreakingVariant_VariantCase_Y = iots.TypeOf<typeof MyFreakingVariant_VariantCase_Y_IO>;
export const MyFreakingVariant_IO = iots.union([
MyFreakingVariant_VariantCase_A_IO,
MyFreakingVariant_VariantCase_X_IO,
MyFreakingVariant_VariantCase_Y_IO,
iots.null,
], 'MyFreakingVariant');
export type MyFreakingVariant = iots.TypeOf<typeof MyFreakingVariant_IO>;
示例6:
type: t.union(LICENSES.map(s => t.literal(s))),
示例7: useAppContext
import { useMemo } from 'react';
import * as t from 'io-ts';
import { useAppContext } from './useAppContext';
// Typed app routing via brilliant gcanti/io-ts.
// Soon in Next.js: https://twitter.com/timneutkens/status/1109092151907045376
const AppHrefIO = t.union([
t.type({ pathname: t.literal('/') }),
t.type({ pathname: t.literal('/me') }),
t.type({ pathname: t.literal('https://twitter.com/steida') }),
t.type({
pathname: t.literal('/web'),
query: t.type({ id: t.string }),
}),
// https://github.com/gcanti/io-ts#mixing-required-and-optional-props
t.intersection([
t.type({ pathname: t.literal('/signin') }),
t.partial({ query: t.type({ redirectUrl: t.string }) }),
]),
]);
export type AppHref = t.TypeOf<typeof AppHrefIO>;
export const useAppHref = () => {
const { router } = useAppContext();
// This should be memoized globally. In serverless, it means per request :-)
const current = useMemo<AppHref | undefined>(() => {
let maybeAppHref: AppHref | undefined;
const routerAppHref = { pathname: router.pathname, query: router.query };