0

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.

2
  • Is Listener part of your codebase or is it imported from a library? Commented Feb 3, 2023 at 16:46
  • It's a part of the codebase - not imported :) Commented Feb 3, 2023 at 17:10

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.