我有以下代码,并收到以下错误
Type 'T' does not satisfy the constraint 'string | number | symbol'. Type 'T' is not assignable to type 'symbol'.
我想知道如何修复泛型,这样我就可以传递一个联盟记录
export type Project<T> = Readonly<{
dirs: Record<T, string>
setup: () => Promise<string>
}>
type Dirs = 'a' | 'b' | 'c'
type Start = Project<Dirs>发布于 2020-11-05 16:34:13
如果你需要这个约束,那么你只需要添加它。也许你甚至可以使它更具限制性(例如,删除符号):
export type Project<T extends string | number | symbol> = Readonly<{
dirs: Record<T, string>
setup: () => Promise<string>
}>
type Dirs = 'a' | 'b' | 'c'
type Start = Project<Dirs>还请注意,我不明白您想要对Record<T, string>做什么。Record仅用于在给定的对象类型中保留一些键。你实际上是指dirs: T吗?如果是这样,你可以这样做:
export type Project<T extends string> = Readonly<{
dirs: T
setup: () => Promise<string>
}>
type Dirs = 'a' | 'b' | 'c'
type Start = Project<Dirs>https://stackoverflow.com/questions/64693643
复制相似问题