Skip to content

Type Inference of overloaded signatures and Variadic tuple types #39944

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

Closed
ChuckJonas opened this issue Aug 7, 2020 · 2 comments
Closed

Type Inference of overloaded signatures and Variadic tuple types #39944

ChuckJonas opened this issue Aug 7, 2020 · 2 comments

Comments

@ChuckJonas
Copy link

I've been messing around with the new variadic tuple types and I noticed some odd behavior around overloads and type inference (I may just be doing something incorrect in my types).

I wrote a higher order function that converts the popular "callback" format into a promise. However, if the function is overloaded, the resulting type seems the be the second to last overloaded signature (the one before the actual implementation).

I guess I'm not surprised this doesn't work, but I just wanted to report as I think it's something that other may run into.

TypeScript Version: 4.0.0-dev.20200803

Search Terms:
variadic, tuple, overload
Code

type CallbackErr = Error | string | null;
type Callback<R> = (err: CallbackErr, result: R) => any

type FunctionWithCallback<T extends any[], R> = (...args: [...T, Callback<R>]) => any;

const promisifyCallback = <I extends any[], R>(func: FunctionWithCallback<I, R>) => (...args: I): Promise<R> => {
  return new Promise((resolve, reject) => {
    func(...args, (err: CallbackErr, result: R) => {
      return err ? reject(err) : resolve(result);
    })
  });
}

function readFile(
  file: string,
  cb: (err: Error | null, result: string) => void
): void;
function readFile(
  file: string,
  type: number,
  cb: (err: Error | null, result: string) => void
): void;
function readFile(
  ...args: any
) {
  return;
}

const readFilePromise = promisifyCallback(readFile);

Expected behavior:

The type of readFilePromise would retain the overloads.

Actual behavior:

const readFilePromise: (file: string, type: number) => Promise<string>. If you switch the top 2 signatures of readFile, it will update to const readFilePromise: (file: string) => Promise 🤷‍♂️

Playground Link:

Playground Link

Related Issues:

#35641

@ChuckJonas
Copy link
Author

Seems like the same issue as #35641, but I wanted to still submit because this implementation isn't using conditional types.

Please close if it's a duplicate

@ChuckJonas ChuckJonas reopened this Aug 7, 2020
@ChuckJonas ChuckJonas changed the title Type Inference of overloaded signatures Type Inference of overloaded signatures and Variadic tuple types Aug 7, 2020
@ChuckJonas
Copy link
Author

doesn't actually seem to be related to the Variadic tuple types... guessing this doesn't add any new info

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

1 participant