首页 > 解决方案 > Angular rambda 处理从不可变数组操作返回的只读类型

问题描述

我在使用rambda(与 typescript 兼容的 ramda 克隆)将它们的功能实用程序用于选择器时遇到问题。Rambda 通过将只读返回和输入类型设置为其实用函数来强制执行不变性。

我正在寻找有关更改代码库以使用不可变只读数组的建议。

如果您需要,这是一个有效的堆栈闪电战。

例如。Rambda 的 typescript 定义文件使用。

export function filter<T>(predicate: Predicate<T>): (input: readonly T[]) => readonly T[];

使用标准的命令式代码不需要接口是只读的

const selectFoos = createSelector(
  selectAllFoos,
  foos => foos.filter(R.identity)
)
// => Selector<object, Foo>

如果我R.filter在选择器中使用返回类型是readonly: T[]

const selectFoos = createSelector(
  selectAllFoos,
  R.filter<Foo>(R.identity), // becomes `readonly Foo[]`
)
// => Selector<object, readonly Foo[]>

更改我使用所选 observable 的类型确实可以解决类型错误,但是有没有更好的方法来处理它?

我想在我的新工作中使用 rambda,但不认为一开始就将所有代码更改为只读以适应 rambda 不是一个好主意,而且我不确定如果我随后将该值通过管道传输到我无法控制的其他东西不接受只读接口。

@Component({ ... })
class FooComponent implements OnInit {

  /* without the readonly property there is a type error */
  // $foos: Observable<Foo[]>

  /* readonly property added to interface */
  $foos: Observable<readonly Foo[]>

  constructor(private store: Store<AppState>){}

  ngOnInit(): void {
    this.foos$ = this.store.pipe(select(selectFoos))
  }
}

为任何帮助而欢呼。

标签: angulartypescriptrambda.js

解决方案


推荐阅读