Declaration merging: remove function overloads?
See original GitHub issueLet’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:
- Created 6 years ago
- Reactions:9
- Comments:11 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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)
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.