gpt4 book ai didi

typescript :接口(interface)中的常量

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

如何在 typescript 的接口(interface)中放置一个常量。就像在 Java 中一样:

interface OlympicMedal {
static final String GOLD = "Gold";
static final String SILVER = "Silver";
static final String BRONZE = "Bronze";
}

最佳答案

您不能在接口(interface)中声明值。

您可以在模块中声明值:

module OlympicMedal {
export var GOLD = "Gold";
export var SILVER = "Silver";
}

在即将发布的 TypeScript 中,您将能够使用 const:

module OlympicMedal {
export const GOLD = "Gold";
export const SILVER = "Silver";
}

OlympicMedal.GOLD = 'Bronze'; // Error

关于 typescript :接口(interface)中的常量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26471239/

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