sanctuary-js / sanctuary

:see_no_evil: Refuge from unsafe JavaScript
https://sanctuary.js.org
MIT License
3.04k stars 94 forks source link

Using typescript with Sanctuary? #531

Closed mattgrande closed 6 years ago

mattgrande commented 6 years ago

I guess I'm wondering if I'm doing something foolish here? I'm just getting errors when I try to specify my types. Eg,

import * as S from 'sanctuary';

const parseNumber = (v: any): S.Maybe<number> => {
  const n = parseInt(v, 10);
  return isNaN(n) ? S.Nothing : S.Just(n);
};

This highlights the S in S.Maybe saying "Cannot find namespace 'S'"

I also tried importing the specific things:

import {Nothing, Just, Maybe, maybe} from 'sanctuary';

const parseNumber = (v: any): Maybe<number> => {
  const n = parseInt(v, 10);
  return isNaN(n) ? Nothing : Just(n);
};

... and that says "Cannot find name 'Maybe'." If I change everything to any, it works fine, so Sanctuary is there & working, but not the types.

Versions in package.json:

"@types/sanctuary": "0.14.2",
"sanctuary": "0.14.1",
davidchambers commented 6 years ago

Should it be import S from 'sanctuary' rather than import * as S from 'sanctuary'? I don't know, I'm just suggesting something to try.

davidchambers commented 6 years ago

S.Maybe<number> doesn't look right. S.Maybe is not a TypeScript type constructor.

mattgrande commented 6 years ago

import S from 'sanctuary' works the same as import * as..., from what I'm seeing.

Oh well, thanks for your help.

davidchambers commented 6 years ago

S.Maybe<number> doesn't look right. S.Maybe is not a TypeScript type constructor.

That is the problem. This works:

import * as S from 'sanctuary';

interface Maybe<A> {
  constructor: {
    '@@type': 'sanctuary/Maybe';
  };
}

const parseNumber = (v: string): Maybe<number> => {
  const n = parseInt(v, 10);
  return isNaN(n) ? S.Nothing : S.Just(n);
};

I don't know whether it's possible to import the Maybe interface. You could ask the people who contributed the type definitions to DefinitelyTyped.

mattgrande commented 6 years ago

Cool, will do.

elie222 commented 5 years ago

Running into the same issue. Was this ever resolved?

davidchambers commented 5 years ago

I'm no TypeScript expert, @elie222. Does the following work for you?

interface Maybe<A> {
  constructor: {
    '@@type': 'sanctuary-maybe/Maybe@1';
  };
}
deiwin commented 3 years ago

That didn't work for me, but the following did:

interface Maybe<A> {
  '@@type': 'sanctuary/Maybe';
}