question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow typing event context

See original GitHub issue

Question: How to define context typescript type?

Hey, not sure if this is an h3 question or a lack of typecript knowledge from me.

I am using Nuxt 3 and am trying to add types to my server handlers. I have a simple server handle:

export default defineEventHandler((event) => { // event here is a H3Event object
    const result = event.context.container.resolve()
    return {
      result
    }
})

The event here is a H3Event:

interface H3Event {
    '__is_event__': true;
    event: H3Event;
    req: IncomingMessage;
    res: ServerResponse;
    context: Record<string, any>;
}

And I have a middleware where I do the following:

export default defineEventHandler((event) => {
  event.context.container = {
    resolve: () => {
      return 'ok'
    }
  }
})

I would like to “type” the event parameter, I would like my VSCode editor to be aware that the event parameter contains a context property that contains a resolve property that is a function that returns a string.

I tried extending the H3Event using the following:

declare module 'h3' {
  interface H3Event {
    context: Record<string, any> & {
      resolve: () => string
    };
  }
}

But Typescript gives me the following error:

Subsequent property declarations must have the same type. Property ‘context’ must be of type Record<string, any>…

Is there a way to define the context type of the event parameter in a server middleware?

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
pi0commented, May 24, 2022

Thanks for the issue. I think we could introduce a new EventContext interface for easy extension. /cc @danielroe is this something you would like to work on?


Related:

0reactions
wobsorianocommented, May 21, 2022

@ElMatella the problem with that is that all your middleware will have the context type of each middleware

Read more comments on GitHub >

github_iconTop Results From Across the Web

Events: change, input, cut, copy, paste
Let's cover various events that accompany data updates. Event: change. The change event triggers when the element has finished changing.
Read more >
html catch event when user is typing into a text input
Use the keyup event to capture the value as the user types, and do whatever it is you do to search for that...
Read more >
Handling Events :: Eloquent JavaScript
Each event has a type ( "keydown" , "focus" , and so on) that identifies it. Most events are called on a specific...
Read more >
Element: contextmenu event - Web APIs | MDN
The contextmenu event fires when the user attempts to open a context menu. This event is typically triggered by clicking the right mouse ......
Read more >
Input Events Level 2 - W3C
This specification defines additions to events for text and related input to allow for the monitoring and manipulation of default browser ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found