打字稿声明参考引发错误
我想了解nectare
关键字背后的概念,并且我尝试运行可重复使用的类型(接口)示例:
interface GreetingSettings {
greeting: string;
duration?: number;
color?: string;
}
declare function greet(setting: GreetingSettings): void;
greet({
greeting: "hello world",
duration: 4000
});
但是我遇到了一个错误:entry未定义
Is this the expected behavior of the declare
keyword ?如果是,我该怎么办,我不会遇到此错误?
I want to understand the concept behind the declare
keyword in practice, and I have tried to run Reusable Types (Interfaces) example:
interface GreetingSettings {
greeting: string;
duration?: number;
color?: string;
}
declare function greet(setting: GreetingSettings): void;
greet({
greeting: "hello world",
duration: 4000
});
But I get an error: greet is not defined
Is this the expected behavior of the declare
keyword? If yes, what should I do that I would not get this error?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的。
声明
的目的是告诉Typescript某些东西已经存在以及其形状是什么。例如,在浏览器环境中有一个声明
forsettimeout
,以告诉Typescript浏览器提供该功能。声明
在运行时没有创建任何内容,这纯粹是将输入类型的类型信息与其他地方创建的内容相关联。 (我想说这不是很惊人的记录[或者我找不到],但是部分涵盖了在这里以及手册的其他地方。-files / 存在,因此将其调用会给您带来运行时错误。
Yes. The purpose of
declare
is to tell TypeScript that something already exists and what its shape is. For example, there's adeclare
forsetTimeout
in the browser environment to tell TypeScript that the browser provides that function.declare
doesn't create anything at runtime, it's purely to associate type information for TypeScript with something created elsewhere. (I'd say this isn't astonishingly-well documented [or I just can't find it], but it is covered in part here and elsewhere in the part of the handbook.)In your case, it would appear that
greet
does not already exist, so calling it gives you a runtime error.