angular - Angular rambda 处理从不可变数组操作返回的只读类型
问题描述
我在使用rambda(与 typescript 兼容的 ramda 克隆)将它们的功能实用程序用于选择器时遇到问题。Rambda 通过将只读返回和输入类型设置为其实用函数来强制执行不变性。
我正在寻找有关更改代码库以使用不可变只读数组的建议。
如果您需要,这是一个有效的堆栈闪电战。
- /app/courses/reducers/courses.reducer.ts
- /app/courses/components/course-list/course-list.component.ts
例如。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))
}
}
为任何帮助而欢呼。
解决方案
推荐阅读
- javascript - JavaScript getter 方法的意义何在?
- android - Flutter:是否可以从其他脚本中的状态运行函数
- angular - 如何为 nb-route-tabset/ngx-admin 设置 activeLinkOptions
- git - Git覆盖master并保留历史
- raspberry-pi - 无法将 Raspberry Pi Camera v1/2 与 Gumstix Pi 计算开发板一起使用
- android-studio - 设置字体自定义searchview Android studio
- visual-studio-code - 如何在整个文件夹上运行 VSCode Code Spell Checker 扩展?/
- wordpress - 如何使用 wordpress 编译 sage9 主题
- excel - 如何保存嵌入的txt文件?
- java - MapStruct 问题绑定到具有 getter 和 Builder 类的类