Skip to content

Creating Type-safe ClsService using custom Module #70

Answered by Papooch
MaheeGamage asked this question in Q&A
Discussion options

You must be logged in to vote

You just inject MyClsService the same way you would inject ClsService. It will be typed with your MyClsStore. Of course, MyClsModule must be imported in the module where MyClsService is needed.

You do that in addition to registering it in for-root. With this method, you're basically just aliasing ClsService to MyClsService to get type inference, but the same ClsService is still used underneath.

If you want type safety even in the setup method, you can type it like so:

setup: (cls: MyClsService, req) => {
  cls.set('userId', req.headers['x-user-id']);
},

Replies: 1 comment 1 reply

Comment options

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

Answer selected by MaheeGamage
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