V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
emilll
V2EX  ›  TypeScript

问一个 ts 类型提取问题

  •  
  •   emilll · 2021-04-15 01:38:57 +08:00 · 1489 次点击
    这是一个创建于 1100 天前的主题,其中的信息可能已经有所发展或是发生改变。
    interface Article {
      filename: string
      title: string
      date: string
    }
    
    const articles: Article[] = [
      {
        filename: 'article1',
        title: 'first article',
        date: '2021-04-13T18:10:40.578Z'
      },
      {
        filename: 'article2',
        title: 'second article',
        date: '2021-04-13T19:32:39.556Z'
      }
    ]
    
    // 从 articles 的 filename 字段提取类型
    // 结果 => type ArticleFilename = 'article1' | 'article2'
    type ArticleFilename = ???
    
    5 条回复    2021-04-15 19:25:06 +08:00
    emilll
        2
    emilll  
    OP
       2021-04-15 06:25:12 +08:00
    @codehz 哇,谢谢大佬
    emilll
        4
    emilll  
    OP
       2021-04-15 18:13:38 +08:00
    @orzfly 这个思路可以啊,用函数做代理提取类型。从结果来看很完美了
    ryougifujino
        5
    ryougifujino  
       2021-04-15 19:25:06 +08:00
    学习了,一开始还以为不行。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1159 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 18:25 · PVG 02:25 · LAX 11:25 · JFK 14:25
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.