I am getting some JSON objects from a network call, of the shape:
{
timestamp: "1636814680595",
price: "$1",
...manyOtherKeys
}
All values are strings.
I'd like to create an interface based on an array of the expected keys. From this:
const expectedKeys = ['timestamp', 'price', ...]
I would like to programmatically generate the type (or interface):
type ExpectedType {
timestamp: string,
price: string
}
Is there an easy way to do that? For now, all I can think of is the following, which seems contrived:
// I did not find a way to start from an array and reduce it to this object
// while preserving the type. I'm okay starting with this object instead of
// an array of string.
const objectWithExpectedShape = {
timestamp: '',
price: '',
}
type ExpectedType = typeof objectWithExpectedShape;