Typings for custom properties in AsyncHook Context #2083
-
Hey @Romakita ! I'm faced with some problems of typings for my custom properties in the AsyncHook Context. I have middleware: import { Middleware, MiddlewareMethods, PlatformContext } from '@tsed/common';
import { MyFooBar } from './MyFooBar';
@Middleware()
export class MyFooBarMiddleware implements MiddlewareMethods {
use(@Context() $ctx: PlatformContext) {
$ctx.set('foo', new MyFooBar());
}
} and I use context in service: import { PlatformContext, Service } from '@tsed/common`;
import { InjectContext } from '@tsed/di';
@Service()
export class MyFooBarService {
@InjectContext()
context: PlatformContext;
myMethod() {
const bar = this.context.foo.bar();
}
} so, when I use property from context I'd like to have typings for it. If it was Context, I would add my custom property to interface like declare global {
namespace TsED {
interface Context {
foo: MyFooBar;
}
}
} but I inject PlatformContext, so it doesn't work. declare global {
namespace TsED {
interface Context extends PlatformContext {
}
}
} Could you please give some pieces of advice in which way I can solve my issue? P.S. I use the latest 6.x version of framework |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hello @NachtRitter this exemple is wrong: import { PlatformContext, Service } from '@tsed/common`;
import { InjectContext } from '@tsed/di';
@Service()
export class MyFooBarService {
@InjectContext()
context: PlatformContext;
myMethod() {
const bar = this.context.foo.bar();
}
} It should be: import { PlatformContext, Service } from '@tsed/common`;
import { InjectContext } from '@tsed/di';
@Service()
export class MyFooBarService {
@InjectContext()
context: PlatformContext;
myMethod() {
const bar = this.context.get<MyFooBar>('foo').bar();
}
} Maybe my example answer you ;) See you |
Beta Was this translation helpful? Give feedback.
Hello @NachtRitter
You shouldn't do that :)
this exemple is wrong:
It should be:
Maybe my example answer you ;)
See you
Romain