【问题标题】:typescript mapping type does not fit打字稿映射类型不适合
【发布时间】:2022-08-17 11:04:50
【问题描述】:
type A = { a: number, b: null } | { a: null, b: number };

const aaa: A[] = [{ a: 1, b: null }, { a: null, b: 1 }];


function ccc(props: A) {

}

aaa.map(temp => {
    ccc({ a: temp.a, b: temp.b }) // it comes error
})

https://www.typescriptlang.org/play?#code/C4TwDgpgBAglC8UDeUCGAuKA7ArgWwCMIAnAGigM1wBtqoBfKAH2TSp1vMu3yOIYDcAKCEBjAPZYAzsDSoMsANoBdBFEUoFARi7taDcpr3VdULQ2XCRAMxxZRwAJaSootwAowxcWCmYYAJTIIvQi8qgAdHioYO7AEHhgCAB8wVDprh5GUPGJEaimuWARBAwBQvQBQA

我该如何使用这种情况?

    标签: typescript


    【解决方案1】:

    将原始对象传递给ccc 有效,将其传播到新对象中也是如此。

    ccc(temp)
    
    ccc({ ...temp })
    

    【讨论】:

      猜你喜欢
      • 2019-11-19
      • 1970-01-01
      • 2021-01-26
      • 2022-12-07
      • 2019-12-11
      • 2022-08-16
      • 1970-01-01
      • 2020-06-07
      • 2018-06-16
      相关资源
      最近更新 更多