question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Declaration merging: remove function overloads?

See original GitHub issue

Let’s say one of the libs come with a function like this:

// library:
declare module 'example' {
    interface Animal { }
    export function sayHello(a: Animal);
}

I want to narrow down the type to just Dog. This is my module augmentation:

// app.ts
declare module 'example' {
    interface Dog extends Animal { }
    export function sayHello(a: Dog);
}

This merges the declarations to two function overloads:

sayHello(a: Dog);
sayHello(a: Animal);

I’d like to remove the generic Animal one to get stricter type checking of my code.

I couldn’t find if this has been discussed before but I would find it useful quite often. Some examples would be:

  • express headers (string by default, I’d use union of string literals)
  • yargs args (I’d limit them to args specific to my app)

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:9
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

10reactions
jcalzcommented, Sep 17, 2021

Could someone articulate how negated/subtraction types would allow declaration merging to remove existing overloads? I don’t see the connection (and therefore I don’t see why this is a duplicate of #4183)

3reactions
jcalzcommented, Oct 11, 2017

Sounds like you need to copy the library’s declaration files, modify the declarations you care about, and include/import only the locally-modified declaration files in your code.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Declaration merging: remove function overloads? #19064
This merges the declarations to two function overloads: sayHello(a: Dog); sayHello(a: Animal);. I'd like to remove the generic Animal one to ...
Read more >
Remove previous declaration with module augmentation
I really don't know of any way to subtract specific module type members (here the function overload) from another defined module augmentation.
Read more >
Documentation - Declaration Merging - TypeScript
For the purposes of this article, “declaration merging” means that the compiler merges two separate declarations declared with the same name into a...
Read more >
In defense of interface: Using declaration merging to disable ...
Effective TypeScript: In defense of interface: Using declaration merging to disable "bad parts"
Read more >
no-redeclare - typescript-eslint
This rule extends the base eslint/no-redeclare rule. It adds support for TypeScript function overloads, and declaration merging. How to Use​ .eslintrc.cjs.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found