TWorldIsNButThis

typescript 类型的交并运算问题,官方文档好像也没给交并运算的详细信息,就给了一个基本的用法

  •  
  •   TWorldIsNButThis · Jun 7, 2022 · 1722 views
    This topic created in 1460 days ago, the information mentioned may be changed or developed.
    type IssueExcludeAssigneesField = {
      id: number
      project_id: number
      title: string
      due_date: string | null
    }
    type Assignee = {
      name: string
    }
    
    type Issue = IssueExcludeAssigneesField & ({ assignees: [Assignee] } | { assignees: [] })
    type Issue2 = IssueExcludeAssigneesField & { assignees: [Assignee] | [] }
    
    
    declare function map<T1, T2>(_: (_: T1) => T2): T2
    declare function pick<T>(_: T): Pick<T, keyof T>
    
    map<Issue, Issue>(pick)  // Type '[Assignee]' is not assignable to type '[]'.
    map<Issue2, Issue2>(pick) // OK
    
    declare function mapIssue(_: Issue): Issue2
    map<Issue, Issue>(mapIssue) // OK
    
    type assignees1 = Issue['assignees']
    type assignees2 = Issue2['assignees']
    declare function mapAssignee(_: assignees1): assignees2
    map<assignees1, assignees1>(mapAssignee) // OK
    

    playground

    上面的这段代码为啥第一个 pick 会报错
    后面的两个 map 应该能证明两个 issue 和 assignees 都是可以互相赋值的等价的类型吧

    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   921 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 22:38 · PVG 06:38 · LAX 15:38 · JFK 18:38
    ♥ Do have faith in what you're doing.