Reputation: 519
Is it possible to write this code without using export const
, but rather export async function
?
This is a sample of the code, heavily simplified:
interface Product {
id: string;
name: string;
}
type Listener<Subject, Payload extends object> = (event: {
subject: Subject;
payload?: Partial<Payload>;
}) => Promise<void>;
export const onEvent: Listener<Product, never> = async (event) => {
await new Promise(function (resolve) {
// a simplified example of using the parameters
console.log(event.subject.id, event.subject.name);
setTimeout(resolve, 1000);
});
};
I'm wondering if it's possible to write this in the form of
export async function onEvent ... {
Without breaking it down into, for example
export async function onEvent(event: { subject: Product; payload?: never }): Promise<void> {
It doesn't seem like it after reading the current Typescript docs.
Upvotes: 0
Views: 504
Reputation: 2678
Basically, no there isn't. With the 'function' keyword, the type is derived from the arguments and return type, you can't separately specify the type. You could do
export const onEvent: Listener<Product, never> = async function (event) {...}
but that's about it.
Upvotes: 1