Trying to declare a function, getting error TS2384: Overload signatures must all be ambient or non-ambient Trying to declare a function, getting error TS2384: Overload signatures must all be ambient or non-ambient typescript typescript

Trying to declare a function, getting error TS2384: Overload signatures must all be ambient or non-ambient


declare means that you don't ever intend to provide a value for that variable. It says to the compiler:

"I know this value doesn't exist in the code you can see, but I promise you that it's defined somewhere that is available to this scope, and this is its type"

So there would be no need for declare if you also provide a concrete value. Either declare the function, meaning it's defined somewhere the typescript compiler can't see, or remove the declare line altogether and export the function itself for use wherever you want.


What does ambient overload signature mean?

"ambient" means no implementation exists. This is triggered by the declare keyword. Overload signatures happen when you write the a function type multiple times, with different argument types/return types. By having the function mentioned on two lines, typescript interprets that as a function overload.

What does non-ambient signature mean?

It means that an implementation could exist for this function.

How do I define a declared function without getting this error?

You don't. If you want to write an implementation for a function, do not use the declare keyword for that functions type.


"Ambient" is a declaration that does not define an implementation (e.g. with the declare keyword). See: https://www.typescriptlang.org/docs/handbook/modules.html. The point of ambient type definitions is to define types that come from third party libraries that don't have types or that may exist in the global space. I'm actually not sure of all the details of why you're not allowed to mix ambient and non-ambient declarations, but one may be that you are accidentally overloading an existing third party function that has an ambient definition.

You can define function overloads without declare, but they can still have only one implementation:

// note that this doesn't actually overload anything yetfunction getValue(key:string):any;function getValue(key:string):any{  return key;}