Giter VIP home page Giter VIP logo

Comments (3)

JalilArfaoui avatar JalilArfaoui commented on May 23, 2024

I think it has something to do with the output type, as I could fix my error by forcing it to unknown :

import * as io from 'io-ts';

export type Brand<T, N> = T & { __brand: N };

export type SensorId = Brand<string, 'SensorId'>;

interface SomeInterface {
  id: SensorId;
}

const InterfaceCodec = io.type({
  id: io.refinement(io.string, (id: string): id is SensorId => true) as io.Type<
    SensorId,
    unknown // <- here
  >,
});

const codec: io.Type<SomeInterface, unknown>  /* <- and here */  = InterfaceCodec;

How can I avoid doing that ?β€―

Why is refinement constructor not using string (here) as output type ?β€―

from io-ts.

mlegenhausen avatar mlegenhausen commented on May 23, 2024

You want to define a output interface SomeInterfaceOutput like

interface SomeInterfaceOutput {
  id: string;
}

The io.refinement type will be io.Type<SensorId, string> so you can define your codec like

const codec: io.Type<SomeInterface,  SomeInterfaceOutput>  = InterfaceCodec;

When you now encode your interface type io-ts converts the SensorId back to a string. This is all intended behavior.

from io-ts.

JalilArfaoui avatar JalilArfaoui commented on May 23, 2024

Thank you for your answer.

Oh, OK, I understand that this is just because output type parameter of Type defaults to A …

Actually, I don’t care about the output type interface, I just wanted to validate that my Codec correspond to an existing type, so I wanted to infer it in some way … 

I just found out I can simply use the OutputOf helper instead of unknown :

export type Brand<T, N> = T & { __brand: N };
export type SensorId = Brand<string, 'SensorId'>;

interface SomeInterface {
  id: SensorId;
}

const InterfaceCodec = io.type({
  id: io.refinement(io.string, (id: string): id is SensorId => true),
});

const codec: io.Type<SomeInterface, io.OutputOf<typeof InterfaceCodec>> = InterfaceCodec;

Anyway, thanks for you answer, and thanks for the work of this lib !

from io-ts.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.