lorenzofox3 / zora

Lightest, yet Fastest Javascript test runner for nodejs and browsers

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Bad type-declaration for `throws`

mindplay-dk opened this issue · comments

The type-hint for the throws assertion is somehow incorrect.

image

No matter what argument I pass, it's the same error:

Argument of type 'string' is not assignable to parameter of type '(Function | RegExp) & (string | undefined)'.ts(2345)

The type declaration definitely looks odd:

export type ErrorAssertionFunction =
| ((
fn: Function,
expected: RegExp | Function,
description?: string
) => IAssertionResult<RegExp | Function>)
| ((fn: Function, description?: string) => IAssertionResult<undefined>);

It looks like you were trying to write an overloaded function type?

That would probably look more like this:

type ErrorAssertionFunction = {
    (fn: Function, expected: RegExp | Function, description?: string | undefined): IAssertionResult<RegExp | Function>;
    
    (fn: Function, description?: string | undefined): IAssertionResult<undefined>;
}

Also (and I'm not sure if this is significant, but) the first function could more accurately specify a dependent return-type:

type ErrorAssertionFunction = {
    <TResult extends RegExp | Function>(
      fn: Function,
      expected: TResult,
      description?: string | undefined): IAssertionResult<TResult>;
    
    (fn: Function, description?: string | undefined): IAssertionResult<undefined>;
}

I'm not sure anyone actually uses the return-type for anything? I've never used it myself - but since it is part of the API, the generic form should be more accurate.

you are right 👍