Skip to content

How do I add types to locals #158

Closed Answered by kartikk221
alpap asked this question in Q&A
Discussion options

You must be logged in to vote

Currently both Request and Response locals have the following type

type DefaultRequestLocals = {
    [key: string]: any;
};

Due to this, the best you can do is extend the DefaultRequestLocals and DefaultResponseLocals and then define your types inside of the extended type which then you can overload whenever you consume locals from either object.

Another approach I have seen is where you define Request as an interface and then extend any custom properties inside of it. With this approach, you don't use locals (or I guess you can) and it would look something like this https://github.com/kartikk221/hyper-express-session/blob/3cd2918344b5f40638cb9c60377102dac89ed1df/types/index.d.ts#LL7-L10C6

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@alpap
Comment options

Answer selected by alpap
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants