diegohaz/constate

Types are not inferred properly

timkindberg opened this issue · 6 comments

My hook has properly inferred types, see here, this is the types that my hook has as seen by the Typescript Service.

My hook:
image

But after passing through constate, the new Provider and Hook that are created have no inferred types, so I'm not getting any TS help/completions anywhere.

Constate's hook:
image

Return from constate hook (no types):
image

This should work. Can you create a CodeSandbox demonstrating the issue?

https://codesandbox.io/s/constatetypeinference-eheo9

Hmm it works there... and it works in VS Code... damn maybe a Jetbrains bug. Are you using like a really new TS feature or something?

VS Code Screenshot:
image

I made a Jetbrains issue: https://youtrack.jetbrains.com/issue/WEB-51538

if you can provide any additional context or guesses on that ticket please feel free. My guess is they have to update their TS service or you used some TS 4 feature that PyCharm has trouble with (but not sure why if they use the right TS service).

Ok so not sure if you are open to this, but if you rewrite the types like this, then it works in PyCharm and VS Code, IMO it's a bit cleaner too :)

import * as React from "react";
declare type Selector<Value> = (value: Value) => any;
declare type SelectorHooks<Selectors> = {
    [K in keyof Selectors]: () => Selectors[K] extends (...args: any) => infer R ? R : never;
};

// Here's the change...
// Removed the Hooks type and just did the `extends` check right here
declare type ConstateTuple<Props, Value, Selectors extends Selector<Value>[]> = Selectors["length"] extends 0
  ? [React.FC<Props>, () => Value]
  : [React.FC<Props>, ...SelectorHooks<Selectors>];

declare function constate<Props, Value, Selectors extends Selector<Value>[]>(useValue: (props: Props) => Value, ...selectors: Selectors): ConstateTuple<Props, Value, Selectors>;
export default constate;

Hmm ok that was when I just hacked them real quick from the node_modules folder... but I've got a fork of constate open and I'm having trouble getting the types to behave...

Oddly, the types are working properly in PyCharm when I am in YOUR project's index.ts file:
image

Maybe it's something to do with how the types were exported??

🤦 I had TS 3 running in PyCharm in my project... that's why it worked in your project... cause it picked up the right version. Easy fix. Sorry for the distraction.