gpt4 book ai didi

typescript - 试图声明一个函数,得到错误 TS2384 : Overload signatures must all be ambient or non-ambient

转载 作者:行者123 更新时间:2023-12-03 21:07:51 28 4
gpt4 key购买 nike

我正在声明一个函数说 getValue。然后我定义了这个 getValue。但我收到错误error TS2384: Overload signatures must all be ambient or non-ambient .
环境过载签名是什么意思?非环境签名是什么意思?如何在不出现此错误的情况下定义声明的函数?

declare function getValue(key:string):any;

function getValue(key:string):any{
return key;
}

最佳答案

declare意味着您永远不打算为该变量提供值。它对编译器说:

"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"


所以不需要 declare如果您还提供了具体的值(value)。要么 declare函数,这意味着它被定义在 typescript 编译器看不到的地方,或者完全删除声明行并导出函数本身以供您在任何地方使用。

What does ambient overload signature mean?


“环境”意味着不存在实现。这是由 declare 触发的关键词。 Overload当您使用不同的参数类型/返回类型多次编写 a 函数类型时,会发生签名。通过在两行中提到函数, typescript 将其解释为函数重载。

What does non-ambient signature mean?


这意味着该功能可能存在实现。

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


你没有。如果要为函数编写实现,请不要使用 declare该函数类型的关键字。

关于typescript - 试图声明一个函数,得到错误 TS2384 : Overload signatures must all be ambient or non-ambient,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65584688/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com