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

Using typescript with Sanctuary? #531

Closed
mattgrande opened this issue Apr 18, 2018 · 8 comments
Closed

Using typescript with Sanctuary? #531

mattgrande opened this issue Apr 18, 2018 · 8 comments

Comments

@mattgrande
Copy link

mattgrande commented Apr 18, 2018

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
Copy link
Member

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
Copy link
Member

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

@mattgrande
Copy link
Author

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

Oh well, thanks for your help.

@davidchambers
Copy link
Member

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
Copy link
Author

Cool, will do.

@elie222
Copy link

elie222 commented Aug 14, 2019

Running into the same issue. Was this ever resolved?

@davidchambers
Copy link
Member

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

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

@deiwin
Copy link

deiwin commented Nov 16, 2020

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

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

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

No branches or pull requests

4 participants