Skip to content

Type signature overload is ignored in a place where it makes better sense. #30343

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
ackvf opened this issue Mar 12, 2019 · 9 comments
Closed
Labels
Duplicate An existing issue was already created

Comments

@ackvf
Copy link

ackvf commented Mar 12, 2019

TypeScript Version: 3.4.0-dev.20190312

Search Terms:
ambient declaration overload

Preface:
I've been using these inline console.debug(val) || val statements for ages, but since recently, void cannot be tested for truthiness. My workaround for typescript projects is to include this in my src/index.tsx file, which works great

declare global {
  interface Console {
    debug(message?: any, ...optionalParams: any[]): false;
  }
}

Enough said, my problem is that this technique does not work in javascript projects.

Code:

// src/global.d.ts

interface Console {
  abc(message?: any, ...optionalParams: any[]): false;
  debug(message?: any, ...optionalParams: any[]): false;
}

// later in src/**/SomeComponent.jsx

[1,2].map((v, ix) => console.debug('v', ix, v) || v) // void cannot be tested for truthiness
[1,2].map((v, ix) => console.abc('v', ix, v) || v) // no error

Expected behavior:
Expected is that the overload that returns false when used in place that test for truthiness is used.
When tested with console.abc, as per the interface, it is correctly picked up as returning false.

Actual behavior:

error: An expression of type 'void' cannot be tested for truthiness ts(1345)

Interestingly, after I have switched to ts@next and thus restarted the TS server, it first picks up my ambient declaration, so the code passes, but then it loads the @types\node\globals.d.ts

global-declaration

@ackvf ackvf changed the title Type signature override with no effect Type signature overload with no effect Mar 12, 2019
@j-oliveras
Copy link
Contributor

Duplicate of #28248 and #26262.

As a workaround, use comma operator:

[1,2].map((v, ix) => (console.debug('v', ix, v), v))
[1,2].map((v, ix) => (console.abc('v', ix, v), v))

@DanielRosenwasser DanielRosenwasser added the Duplicate An existing issue was already created label Mar 12, 2019
@DanielRosenwasser DanielRosenwasser changed the title Type signature overload with no effect Express that a void signature has effects and should be usable for in || and && operations Mar 12, 2019
@ackvf
Copy link
Author

ackvf commented Mar 13, 2019

@j-oliveras @DanielRosenwasser
Actually no, I am not upset about void not being tested for truthiness. I am upset about my overload not being applied in a place where the application surroundings require it. If my understanding of overloads is wrong, then I am sorry and I'll be happy to close this issue, but in that case an explanation would be helpful. Thanks in advance.

As you can see, I have seen and commented on that issue that you linked to this one as duplicate:
#26262 (comment) My issue is different.

@ackvf ackvf changed the title Express that a void signature has effects and should be usable for in || and && operations Type signature overload is ignored in a place where it makes better sense. Mar 13, 2019
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@ackvf
Copy link
Author

ackvf commented Mar 16, 2019

Wait @typescript-bot why u do dis?!
This issue isn't a duplicate and I have made a recent activity to express that.

@ackvf
Copy link
Author

ackvf commented Mar 16, 2019

@RyanCavanaugh I believe your bot is malfunctioning, please help!

@RyanCavanaugh
Copy link
Member

@ackvf this is a question about how to get your JS file configured to see the global declaration file. I'd recommend StackOverflow as a good support point. Thanks!

@ackvf
Copy link
Author

ackvf commented Mar 17, 2019

@RyanCavanaugh It does see the global declaration file as demonstrated in the gif, but it chose a different overload even though that overload results in an error.

@RyanCavanaugh
Copy link
Member

Overload resolution doesn't work by iterating each overload to see which makes your entire program not-an-error. The first matching overload is chosen; the return type isn't even part of this process.

@ackvf
Copy link
Author

ackvf commented Mar 17, 2019

Call signature is already considered, but if two return types share one call signature, maybe it should also consider a return type and the surrounding?
Or do you have another advice to force a particular overload or something else to make the compiler not complain?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

5 participants