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

Type definitions? #69

Open
EduardoAraujoB opened this issue Sep 8, 2021 · 2 comments
Open

Type definitions? #69

EduardoAraujoB opened this issue Sep 8, 2021 · 2 comments

Comments

@EduardoAraujoB
Copy link

EduardoAraujoB commented Sep 8, 2021

So, I'm about to start using this lib in a project, but I figured out that there is no type declaration file for the project or a @types/z package

@EduardoAraujoB
Copy link
Author

EduardoAraujoB commented Sep 8, 2021

I also wrote a simple type definition in my project

//  T is the return type and U is the type of the parameter
declare module 'z' {
  type Matches = <T, U>(
    value: U,
  ) => (
    matcher: (value: U) => void,
    matcher?: (value: U) => void,
    matcher?: (value: U) => void,
    matcher?: (value: U) => void,
  ) => T;

  export const matches: Matches;
}

those types are obiviously wrong and there is some problems, one of them is that I need to keep repeating the optional matcher parameter in the type definition file when pattern matching have more matchers than the defined (4 in this case)

@nythrox
Copy link

nythrox commented Mar 2, 2022

This works for values

declare module 'z' {

  type Matcher<U, R> = (value: U) => R
  
  type Matches = <U>(value: U) => <M extends Matcher<U, any>[]>(
    ...matchers: M
  ) => M extends Matcher<infer _, infer R>[] ? R : never;
  
  const matches: Matches;
  
}

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

2 participants