【发布时间】:2021-07-19 23:50:10
【问题描述】:
我想创建一个动态类型,它是接口符号的联合。 给定一个文件“MyInterfaces.ts”
export interface SomeInterfaceA {}
export interface SomeInterfaceB {}
...
export interface SomeInterfaceZ {}
我有以下定义:
import * as AllMyInterfaces from "./MyInterfaces"
type InterfaceNames = keyof typeof AllMyInterfaces
const test: InterfaceNames = "SomeInterfaceA"
//^^^^^^^^^ TS2322: Type 'string' is not assignable to type 'never'.
但是编译器说:类型'string'不能分配给类型'never'
不知何故,“typeof AllMyInterfaces”的结果不包含任何接口符号。 我必须如何定义我的 Union 类型才能使其工作? 谢谢
【问题讨论】:
标签: typescript