#Property 'createDecorator' does not exist on type 'typeof Reflector'.ts

1 messages · Page 1 of 1 (latest)

cobalt berry
#

Here is my version list,

[System Information]
OS Version     : macOS Unknown
NodeJS Version : v18.17.0
PNPM Version    : 8.6.12

[Nest CLI]
Nest CLI Version : 10.0.0

[Nest Platform Information]
platform-express version : 10.0.0
schematics version       : 10.0.0
testing version          : 10.0.0
common version           : 10.0.0
core version             : 10.0.0
cli version              : 10.0.0
onyx thicket
cobalt berry
#

@onyx thicket sorry, I thought this was enough as the error pretty much says what I was trying to do. I can't access the createDecorator property of Reflector. I was following this doc,
https://docs.nestjs.com/guards#setting-roles-per-handler

Reflector.createDecorator was introduced in v10 which I'm using but still can't access the createDecorator property.

onyx thicket
#

ah i see what you're trying to do
just a guard decorator

onyx thicket
cobalt berry
#

This is my guard


@Injectable()
export class AuthGuard implements CanActivate {

  constructor(private reflector: Reflector) { }


  canActivate(
    context: ExecutionContext,
  ): boolean | Promise<boolean> | Observable<boolean> {

    const userTypes = this.reflector.get<UserType[]>('userTypes', context.getHandler())

    if (!userTypes || userTypes.length == 0) {
      return true
    }

    const request = context.switchToHttp().getRequest()

    const user: UserSafe = request.user

    if (!user) {
      return false
    }

    return userTypes.includes(user.type)
  }

}

I'm using it in a controller like this,


@Controller('flight')
@UseGuards(AuthGuard)
export class FlightController {

    constructor(private readonly context: ContextService) { }

    @Get()
    // @Auth('SUPERADMIN')   <---- Can't use this since can't create the decorator at all
    async test(): Promise<string> {
        return "Hello World!";
    }
}

The code mentioned on the doc is not working it self, so can't use this anywhere. createDecorator is highlighted red.

import { Reflector } from '@nestjs/core';

export const Roles = Reflector.createDecorator<string[]>();