z icon indicating copy to clipboard operation
z copied to clipboard

Type definitions?

Open EduardoAraujoB opened this issue 4 years ago • 2 comments

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 avatar Sep 08 '21 14:09 EduardoAraujoB

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)

EduardoAraujoB avatar Sep 08 '21 15:09 EduardoAraujoB

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;
  
}

nythrox avatar Mar 02 '22 15:03 nythrox