gpt4 book ai didi

typescript - 是否有理由在类型和接口(interface)前使用 declare 关键字?

转载 作者:搜寻专家 更新时间:2023-10-30 20:35:32 27 4
gpt4 key购买 nike

我遇到过这样的代码:

declare type A = { ... };
declare interface B { ... }

通过搜索 declare 关键字(出于某种原因,没有任何结果指向我的 TypeScript 文档),当 JavaScript 中存在变量时使用 declare 并且你需要从 TypeScript 中引用它,所以你向编译器声明这个变量确实存在。

我发现所有使用此关键字的示例都像这样使用它:declare var a;

为什么我要在typeinterface 前使用它?根据我对declare的理解,这样做是完全没有用的,但是编译器没有给我任何错误。

最佳答案

看来我刚刚找到原因了。

来自这个 github issue :

declare should be optional on type aliases and interfaces.

declare type and declare interface are identical to type and interface

Since we already shipped type aliases this way, people have had to write declare type a bunch of times. We could break them and say "Write type, not declare type", but that would be a massive break for no real reason.

总而言之,没有理由对类型和接口(interface)使用declare

关于typescript - 是否有理由在类型和接口(interface)前使用 declare 关键字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49156130/

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