I was looking up the Record type on typescriptlang.org. What would be the best way to convert this type to rescript keeping in mind interop, for example a binding.
interface CatInfo {
age: number;
breed: string;
}
type CatName = "miffy" | "boris" | "mordred";
const cats: Record<CatName, CatInfo> = {
miffy: { age: 10, breed: "Persian" },
boris: { age: 5, breed: "Maine Coon" },
mordred: { age: 16, breed: "British Shorthair" },
};
cats.boris;
If I am reading this right, the record type has to have one of the names from type CatName
and has to have the properties of type Catinfo
.
Can we do this type of construction?
I came up with this but doesnt seem quite the same thing, playground:
type catInfo = {
age: int,
breed: string,
}
type record =
| Miffy(catInfo)
| Boris(catInfo)
| Mordred(catInfo)
let miffy = Miffy({age: 10, breed: "Persian"})
let boris = Boris({age: 5, breed: "Maine Coon"})
let mordred = Mordred({age: 16, breed: "British Shorthair"})
let cats: array<record> = [miffy, boris, mordred]
Js.log(cats)
and logs this:
[
{ TAG: 0, _0: { age: 10, breed: 'Persian' } },
{ TAG: 1, _0: { age: 5, breed: 'Maine Coon' } },
{ TAG: 2, _0: { age: 16, breed: 'British Shorthair' } }
]
ts version logs, playground:
{
"miffy": {
"age": 10,
"breed": "Persian"
},
"boris": {
"age": 5,
"breed": "Maine Coon"
},
"mordred": {
"age": 16,
"breed": "British Shorthair"
}
}
Or logging console.log(cats.boris)
in ts:
{
"age": 5,
"breed": "Maine Coon"
}
Which my structure doesnt let us do.
Any musings on this type of thing would be appreciated.
Thank you.