Comments (1)
The problem is that union
accepts at least two elements else creating a union makes no sense as you could for one element use the codec directly and you need to define what needs to happen when the array is empty.
In this case also the const
does not make sure that you have at least two element as the map
function converts your triple back to an array. You can get this working by using map
only on the "rest" of your array and make sure that you have at least two elements. This looks something like this:
import * as t from 'io-ts';
const myEnumValues = [
'value_a',
'value_b',
'value_c',
] as const;
export type MyEnumValue = typeof myEnumValues[number];
const [first, second, ...rest] = myEnumValues;
export const tMyEnumValue = t.union([
t.literal(first),
t.literal(second),
...rest.map((value) => t.literal(value))
]);
If you want to support less that two elements but at least one you could write your own union
function that
import * as t from 'io-ts'
import * as NEA from 'fp-ts/NonEmptyArray'
export const relaxedUnion = <A extends NEA.NonEmptyArray<t.Mixed>>(
codecs: A,
name?: string,
): t.Type<t.TypeOf<A[number]>, t.OutputOf<A[number]>> =>
pipe(
codecs,
NEA.matchLeft((head1, tail) =>
pipe(
tail,
A.matchLeft(
() => head1,
(head2, tail2) => t.union([head1, head2, ...tail2], name),
),
),
),
);
from io-ts.
Related Issues (20)
- Behavior changes and types are incorrect based on `intersection` array order
- Intersection with record whose keys are a custom type HOT 1
- io-ts recursion use issue HOT 4
- Subpath imports in ESM mode HOT 5
- Clarification question - how to work with the type of codecs themselves? HOT 1
- Surprising acceptance of various inputs HOT 1
- ReadonlyNonEmptyArray can't be used in a mapped type HOT 1
- How to generate documentation for types generated with `t.TypeOf` HOT 1
- Difficulties with generic serialisable type HOT 2
- [Question] Typing a generic mapped union HOT 1
- Inference error for `toString` property in intersection types with TypeScript 4.9.5 HOT 1
- `t.TypeOf<keyof<o>>` should return a string union type, not a numeric union type.
- PSA: TS 5.1 can break using this library HOT 1
- Intersection of Function with Object doesn't validate correctly
- t.Int: use Number.isSafeInteger instead of Number.isInteger
- non-enumerable records with extra keys do not pass `io-ts.record.is`, contrary to TypeScript types HOT 12
- partially enumerable record missing enumerable keys passes `record.is`, contrary to TypeScript types
- Records are stripped when encoding with t.record(NumberFromString, xxx) HOT 2
- Question: Convert a form to a type with different structure
- Incorrect behaviour when used with browser geolocation coordinates object
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from io-ts.