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

Function argument Type-assertion does not work correctly #31716

Closed
manuth opened this issue Jun 1, 2019 · 3 comments
Closed

Function argument Type-assertion does not work correctly #31716

manuth opened this issue Jun 1, 2019 · 3 comments
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed

Comments

@manuth
Copy link
Contributor

manuth commented Jun 1, 2019

TypeScript Version: 3.6.0-dev.20190601

Search Terms:

  • Function argument
  • Type-Assertion
  • Argument-Types
  • promisify
  • Function-overloads
  • Overloads

Code

declare function sourceMaps(): NodeJS.ReadWriteStream;
declare function sourceMaps(path: string): NodeJS.ReadWriteStream;
declare function sourceMaps(path: string, options: object): NodeJS.ReadWriteStream;
declare function sourceMaps(options: object): NodeJS.ReadWriteStream;

declare function queue<TArgs extends any[]>(fn: (...args: TArgs) => NodeJS.ReadWriteStream, ...args: TArgs): any;

declare function altQueue(fn: () => NodeJS.ReadWriteStream): any;
declare function altQueue<TArg1>(fn: (arg1: TArg1) => NodeJS.ReadWriteStream, arg1: TArg1): any;
declare function altQueue<TArg1, TArg2>(fn: (arg1: TArg1, arg2: TArg2) => NodeJS.ReadWriteStream, arg1: TArg1, arg2: TArg2): any;

queue(sourceMaps);              // Reports an error
queue(sourceMaps, "");          // Reports an error
queue(sourceMaps, "", {});      // Reports an error
queue(sourceMaps, {});

altQueue(sourceMaps);
altQueue(sourceMaps, "");       // Reports an error
altQueue(sourceMaps, "", {});   // Reports an error
altQueue(sourceMaps, {});

Expected behavior:
TypeScript should recognize there is a function-overload which matches the TArgs- or TArg1- and TArg2-type.

Actual behavior:
TypeScript reports an error expecting the argument-types to match the last declaration of the function.

Playground Link:
Link to Playground

This issue is, for example, affecting the promisify-function of node.js' util-module:

import { promisify } from "util";

declare function test(callback: (error: Error, result: any) => any): void;
declare function test(name: string, callback: (error: Error, result: any) => any): void;
declare function test(options: { name: string, author: string }, callback: (error: Error, result: any) => any): void;

promisify(test)();
promisify(test)("");                        // Reports an error
promisify(test)({ name: "", author: "" });  // Reports an error
@jcalz
Copy link
Contributor

jcalz commented Jun 2, 2019

Related to or duplicate of #30369, #29312 ?

Perhaps you mean "type inference" and not "type assertion"? Or at least I don't see any type assertion happening here. Also it seems that "overload" should be part of any search terms.

@manuth
Copy link
Contributor Author

manuth commented Jun 2, 2019

I'm not that sure whether this is the same issue but it sure does look a little similar, that's true

@RyanCavanaugh RyanCavanaugh added the Design Limitation Constraints of the existing architecture prevent this from being fixed label Jun 13, 2019
@RyanCavanaugh
Copy link
Member

We can't yet support higher-order overload resolution, which is what all of these would require. The linked issues go into more detail

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed
Projects
None yet
Development

No branches or pull requests

3 participants