I have a library "foo" that implements a function "bar" like this

declare const bar = (baz: string) => void

I want to override that type to be something different, like

declare const bar = (baz: number) = void

So I do this

declare module "foo" {
  declare const bar = (baz: number) = void
}

But when I do this, I just end with a function overload and it allows both number and string to be passed.

How can I make that function be of the type I want to "override" without creating a function overload?

1 Reply 1

This is syntactically invalid pseudocode. And you can't redeclare consts at all; it doesn't result in a merge, just an error about redeclaration of a const. Please fix this to be a minimal reproducible example and maybe someone can help.

That being said, you cannot replace declarations, only merge with them. And merging a function or a method (but not a const) results in an overload. So assuming I do understand what you're asking for, it's impossible. You would need to locally fork/modify/replace the declaration in question. Maybe if ms/TS#19064 were ever implemented you could remove the existing type and add a new one. But it's not implemented, so it's not possible at all.

Your Reply

By clicking “Post Your Reply”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.