首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >类型'T‘不满足约束'string | number | symbol’。类型“T”不能赋值给类型“symbol”

类型'T‘不满足约束'string | number | symbol’。类型“T”不能赋值给类型“symbol”
EN

Stack Overflow用户
提问于 2020-11-05 16:30:30
回答 1查看 1.9K关注 0票数 0

我有以下代码,并收到以下错误

Type 'T' does not satisfy the constraint 'string | number | symbol'. Type 'T' is not assignable to type 'symbol'.

我想知道如何修复泛型,这样我就可以传递一个联盟记录

代码语言:javascript
复制
   export type Project<T> = Readonly<{
      dirs: Record<T, string>
      setup: () => Promise<string>
    }>
    
    type Dirs = 'a' | 'b' | 'c'
    
    type Start = Project<Dirs>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-05 16:34:13

如果你需要这个约束,那么你只需要添加它。也许你甚至可以使它更具限制性(例如,删除符号):

代码语言:javascript
复制
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吗?如果是这样,你可以这样做:

代码语言:javascript
复制
export type Project<T extends string> = Readonly<{
      dirs: T
      setup: () => Promise<string>
    }>
    
    type Dirs = 'a' | 'b' | 'c'
    
    type Start = Project<Dirs>
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64693643

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档