Here's a function I have:
const plainText = (text: string) => ({
type: 'plain_text',
text,
emoji: true,
});
Currently, the inferred return type is:
const plainText: (text: string) => {
type: string;
text: string;
emoji: boolean;
}
Is there a way to make it return with the type of type
being 'plain_text'
instead of string
? Or do I need to manually specify the return type?
const plainText: (text: string) => {
type: 'plain_text';
text: string;
emoji: boolean;
}
I discovered that I can do it this way, but is it the most optimal approach?
const plainText = (text: string) => ({
type: 'plain_text' as 'plain_text',
text,
emoji: true,
});
I also have other functions like this:
const text = (text: string) => ({
type: 'section',
text: {
type: 'mrkdwn',
text,
},
});
I'm creating these functions to represent some common Slack Block types that I frequently use, allowing me to simply pass them with something like plainText('hello')
. These functions are static and only the text content changes.
The issue arises when I use them with a Slack Client, which demands the type to be exactly type: 'plain_text'
rather than type: 'string'
. Type definition