【发布时间】:2023-02-21 23:10:03
【问题描述】:
我正在使用一个具有如下枚举的库
enum TestEnum {
FIRST = 0,
SECOND = 100,
THIRD = 200,
FOURTH = 300,
}
它没有被导出,而是在另一个类类型中使用,如下所示
declare class TestClass {
testEnumProp: TestEnum | string | null;
}
现在我自己的代码需要那个枚举,但我不能直接使用它,因为它没有直接导出。所以我尝试了类似下面的方法来提取类型
type TestEnumType = Exclude<NonNullable<TestClass['testEnumProp']>,string>
在以下情况下有效
const val:TestEnumType = TestEnum.FIRST;
但是因为我不能直接访问TestEnum,所以我不能使用TestEnum.FIRST并且必须使用我提取的类型
const val2:TestEnumType = TestEnumType.FIRST;
但它不起作用,因为 TestEnumType 是一种类型,而不是枚举本身。这是我在 IDE 上收到的错误。
'TestEnumType' only refers to a type, but is being used as a value here.ts(2693)
知道我是否可以通过某种 TS 魔法以某种方式使用 TestEnumType.FIRST 吗?
【问题讨论】:
-
你用的是什么图书馆?
标签: javascript typescript enums typescript-types