Questions tagged [io-ts-library]
15 questions
6
votes
1 answer
How to transform a JS array of strings into a union with io-ts?
I'm using io-ts and i'm wondering if there's a way to turn an array of strings (literals) into a union of such literals. For example:
export const CONTROLS = [
"section",
"text",
"richtext",
"number",
];
export const ControlType = t.union(
…

Obed Parlapiano
- 3,226
- 3
- 21
- 39
6
votes
2 answers
Io-ts interface for properties with unknown keys
I'm trying to create an io-ts interface of the following:
export interface myInterface {
[key:string]?: string | undefined | null
}
I want to turn this into the io-ts equivalent. The end goal is to combine it with another existing io-ts…

jbailie1991
- 1,305
- 2
- 21
- 42
2
votes
1 answer
io-ts: Using a literal union, define required and optional keys in an object
I'm trying to define a new codec with io-ts.
The shape should look like the following when I'm done:
type General = unknown;
type SupportedEnv = 'required' | 'optional'
type Supported = {
required: General;
optional?: General;
}
(Note: for the…

StephenWeiss
- 873
- 1
- 8
- 17
2
votes
1 answer
convert or filter a `Task` from `Some` to `Left` with `fp-ts`
I'm trying to learn how to use type guards and predicted functions with io-ts | fp-ts, and what I need to do the following:
I've this function:
createOne: ({ password, ...creatableUser }: CreatableUser): taskEither.TaskEither

Puszkarek
- 63
- 5
2
votes
1 answer
io-ts logging decoding errors for union types in arrays
I am using io-ts and trying to decode nested arrays of values. The default io-ts behavior is that if any item in an array fails, it fails the whole array. I still wanted the array to pass and only return the valid items and be able to log the…

Brandon
- 187
- 13
2
votes
1 answer
Get union codec from type values in io-ts
I am trying to create union codec from type codec in io-ts.
What I am trying to achieve is basically transforming from:
import * as t from 'io-ts'
const FilterTypeC = t.type({
POTATO: t.literal('POTATO'),
CABBAGE:…

Bartosz Kwiatek
- 21
- 3
2
votes
1 answer
How to define mixed array in io-ts?
Using latest io-ts, I would like to model property result of NodeLsStatusResponse to contains objects of type NodeStatus or NodeStatus404 in (t.readonlyArray)
How to define this relationship with io-ts?
export const Connection =…

GibboK
- 71,848
- 143
- 435
- 658
2
votes
1 answer
Can I use a TypeScript type with io-ts?
I am working on validating API responses using io-ts.
I have already defined the following type in regular TypeScript:
export type Group = {
id: number;
name: string;
}
And I want to use this type in io-ts like this:
const UploadedSearch =…

green
- 313
- 3
- 10
1
vote
1 answer
How to correctly type a function in io-ts
I have the following:
export const ObjC = Codec.struct({
name: Codec.string,
value: Codec.number,
})
export type ObjType = Codec.TypeOf
I want a function for decoding this object and returning an Error (not DecoderError). Similar…

Milk
- 2,469
- 5
- 31
- 54
0
votes
0 answers
How to get the raw type of an io-ts codec?
I have an io-ts codec that is defined like this:
const userCodec = t.type({
id: UUIDCodec,
pass: passCodec
})
export type User = t.TypeOf
I want to know if it's possible to derive this type to get a type like {id: string,…

packenbush
- 90
- 1
- 8
0
votes
1 answer
io ts, check if a string is in a union
Let's say I have the following io-ts runtimetype:
const Example = t.keyof({
'foo': null,
'bar': null
})
type ExampleType = typeof Example
And I have an incoming request with value: string
How can I convert value to an…

Abraham P
- 15,029
- 13
- 58
- 126
0
votes
0 answers
How to specify that a value should be a class, which has a certain superclass, especially with io-ts?
I am using io-ts to parse objects from JSON that can be any of a known variety of types. Each object comes in with a string key @type specifying what its type is. I would like to keep a Record/map of type names to io-ts type "class objects" so i can…

qqq
- 1,360
- 1
- 9
- 24
0
votes
0 answers
io-ts recursion use issue
I trying to use t.recursion of io-ts library to document a type, that has recursion.
The type in documentation looks like this:
export interface BoxBase {
id?: string;
mode: DockMode;
size?: number;
children: (BoxBase | PanelBase)[];
}
I…

Mrmld Sky
- 139
- 1
- 7
0
votes
1 answer
io-ts how to use a custom union as type to be checked at run time
I would like to use a custom union, or generally a custom type in io-ts.
I receive an error in this like
T.array(T.type(MyUnion))
Could you please tell me what is the right way to achieve this?
import * as T from 'io-ts';
import * as E from…

Radex
- 7,815
- 23
- 54
- 86
0
votes
1 answer
parser-ts: simple many parser goes into infinite loop
Trying to understand how parsers work in parser-ts, but encountered a pretty unexpected behaviour, a simple P.many parser run on a string just hangs for ever, what am I doing wrong?
const everything = pipe(
Ch.alphanum,
P.alt(() =>…

dark_ruby
- 7,646
- 7
- 32
- 57