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

Bad type-declaration for throws #134

Closed
mindplay-dk opened this issue Jan 31, 2022 · 1 comment
Closed

Bad type-declaration for throws #134

mindplay-dk opened this issue Jan 31, 2022 · 1 comment

Comments

@mindplay-dk
Copy link
Contributor

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.

@lorenzofox3
Copy link
Owner

you are right 👍

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

2 participants