我正在尝试用 rescript 模拟写入 DB 的副作用。
所以我想在调用时将数据推送到数组中repository.add
。Js.Array.push
返回一个int
,我不在乎。我想强制返回unit
,以便我的签名显示unit
,这让我立即知道这个函数会产生副作用。
这是代码(这里是游乐场):
module Person = {
type entity = {
firstName: string
}
type repository = {
add: entity => unit,
getAll: unit => array<entity>
}
let createRepository = (): repository => {
let storage: array<entity> = []
{
add: entity => {
Js.Array.push(entity, storage) // This has type: int -> Somewhere wanted: unit
() // how to force to return 'unit' there ?
},
getAll: () => storage
}
}
}