I am trying to write an interface for a specific function that will take in an object and perform some intended functionality. I would like to type the input to ensure it follows a specific shape.
When I go to build such an interface, I would expect my code to work, but at compile it complains, “Type ‘string’ is not assignable to type ‘”message”‘.”
interface BodyInterface {
settings?: any;
body?: Array<Message>;
}
interface Message {
type: MessageTypeType;
text: string;
}
type MessageTypeType = "message";
const inputBody = {
body: [
{
type: 'message',
text: 'hello world'
}
]
}
const caller = (body: BodyInterface) => {
console.log(body)
}
caller(inputBody)
How can I declare this interface so that the body
is expected to be an Array with contents that are objects with type: 'SOME_EXPLICIT_VALUE'
?