Hej, chciałbym aby przekazywany do sendMessage() argument message mógł być tylko typu któregoś z parametrów obiektu messages.
Mam javascriptowy obiekt message:
const messages: IMessages = {
first: 'The first message',
second: (txt: string) => 'The second message' + txt
}
interface IMessages {
first: string;
second: (txt: string) => string;}
I chciałbym przekazywać własności tego obiektu w funkcji:
const sendMessage = (message) => {
[...]
}
i wywoływać ją w taki sposób
sendMessage(messages.first)
Próbowałem np stworzyć typ dla parametrów wiadomości:
export type myMessage = string | ((...args: unknown[]) => string);
i wtedy interfejs dla messages i funkcja sendMessage wyglądałyby tak:
interface IMessages {
first: MyMessage;
second: MyMessage;
}
[...]
const sendMessage = (message: MyMessage) => ...
Ale takie rozwiązanie nie działa kiedy przekazuje w argumencie własność, która jest funkcją, np:
sendMessage(messages.second('jakis teskt'))
Dostaje taki błąd
TS2349: This expression is not callable.
Not all constituents of type 'myMessage' are callable.
Type 'string' has no call signatures.
Dla message.frist, który jest stringiem ten błąd się nie pojawia.