Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: Types on interface vs. types on instance #1092

Closed
jloleysens opened this issue Jan 16, 2020 · 4 comments
Closed

Question: Types on interface vs. types on instance #1092

jloleysens opened this issue Jan 16, 2020 · 4 comments

Comments

@jloleysens
Copy link

Guidance here would be appreciated :)

Current Behavior

Looking at the type declaration of Either in the .d.ts shipped with the npm package I am seeing:

/**
 * @since 2.0.0
 */
export interface Left<E> {
    readonly _tag: 'Left';
    readonly left: E;
}
/**
 * @since 2.0.0
 */
export interface Right<A> {
    readonly _tag: 'Right';
    readonly right: A;
}
/**
 * @since 2.0.0
 */
export declare type Either<E, A> = Left<E> | Right<A>;

But either instance is defined as:

export declare const either: Monad2<URI> & Foldable2<URI> & Traversable2<URI> & Bifunctor2<URI> & Alt2<URI> & Extend2<URI> & ChainRec2<URI> & MonadThrow2<URI>;

Which contains Applicative and so Functor meaning it can map but this code currently contains a type error:

import { Either } from 'fp-ts/lib/Either';
const myValidationHandler = (myEither: Either<any, any>) => {
  myEither./* error --> */map(() => {});
};

Message:

Property 'map' does not exist on type 'Either<any, any>'.
  Property 'map' does not exist on type 'Right<any>'.

Expected behaviour

I would have expected the types as defined on the instance to be shared with the types as defined on the interface (per the example above). Or am I using the interfaces incorrectly? Should I rather be using Functor directly? The map functionality seems to be present at runtime though.

Your environment

Which versions of fp-ts are affected by this issue? Did this work in previous versions of fp-ts?

Software Version(s)
fp-ts 2.3.1
TypeScript 3.7.2
@mlegenhausen
Copy link
Collaborator

const either is defined as Functor not type Either.

Your corrected code is

import { Either, either } from 'fp-ts/lib/Either';
const myValidationHandler = (myEither: Either<any, any>) => {
  either.map(myEither, () => {});
};

@gcanti
Copy link
Owner

gcanti commented Jan 16, 2020

Or

import { Either, map } from 'fp-ts/lib/Either'
import { pipe } from 'fp-ts/lib/pipeable'

const myValidationHandler = (myEither: Either<any, any>) =>
  pipe(
    myEither,
    map(() => {})
  )

if you prefer data-last functions

@gcanti gcanti closed this as completed Jan 16, 2020
@jloleysens
Copy link
Author

jloleysens commented Jan 16, 2020 via email

@mlegenhausen
Copy link
Collaborator

This was the version 1 behavior of the library where every type was a class with all methods attached. The change was made cause of disadvantages when using classes like serialization, extendibility and tree shaking problems. You can find the full discussion in #823

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants