gpt4 book ai didi

import - typescript 中的 `from foo import *`

转载 作者:行者123 更新时间:2023-12-04 16:59:21 29 4
gpt4 key购买 nike

我正在开发一个相当小的 Typescript 代码库,该代码库已经足够大,可以拆分到多个文件中。这是一个二十一点游戏。我目前有一堆代码,看起来像:

var player = new Player();
player.hand = new Hand([new Card(), new Card(), new Card()]);

在我的主文件中, app.ts .为了我的理智,我想拆分 Player将类(class)分成一个模块和 HandCard一秒钟。但是,我想保留 Player , Card , 和 Hand出于两个原因,所有这些都在顶级范围内可用。首先,避免以下情况:
var player = new Players.Player();
player.hand = new Cards.Hand([new Cards.Card(), new Cards.Card(), new Cards.Card()]);

其次,因为我认为它暗示了 Cards 之间的层级关系。和 Hand我不想建议。 Cards包含类似 Card 的内容, Deck , Hand等——我认为这是最明智的名字选择(而不是像 CardCollections 这样的名字),但阅读 Cards.Hand让我感觉像 Hand is-a Cards ,它不是。

在 Python 中,我会写:
from Cards import Card, Hand, Deck
from Players import Player

或者
from Cards import *
from Players import *

The Typescript handbook本质上,建议您为所需的所有内容创建别名,但在我看来,您仍然无法将任何内容导入顶级命名空间。

这在 typescript 中存在吗?

最佳答案

that you make aliases for everything you need, but it looks to me like you still can't import anything into the top-level namespace



不是全局全局命名空间而是文件中的顶级命名空间,您可以执行以下操作:
var Hand = Card.Hand; 
var Player = Players.Player;

关于import - typescript 中的 `from foo import *`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24443264/

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