1

It seems that babel/plugin-proposal-decorators is not converting the tsyringe inject() and injectable() to javascript. This is the script that i'm using to convert my typescript files to javascript:

"build": "babel src --extensions \".js,.ts\" --out-dir dist --copy-files --no-copy-ignored"

This is my babel.config.js:

module.exports = {
  presets: [
    [
      '@babel/preset-env',
      {
        targets: {
          node: 'current',
        },
      },
    ],
    '@babel/preset-typescript',
  ],
  plugins: [
    [
      'module-resolver',
      {
        alias: {
          '@modules': './src/modules',
          '@config': './src/config',
          '@shared': './src/shared',
        },
      },
    ],
    ['@babel/plugin-proposal-decorators', { legacy: true }],
  ],
  ignore: ['**/*.spec.ts'],
};

And this is the error that returns to me in terminal:

TypeInfo not known for "CreateUserService"

This is my CreateUserService.ts file. This is a service that I use to create a new user in my application, and I use two tsyringes containers (usersRepository and hashProvider). I am using TypeORM as my ORM:

import 'reflect-metadata';
import { injectable, inject } from 'tsyringe';

import AppError from '@shared/errors/AppError';
import User from '@modules/users/infra/typeorm/entities/User';

// Interfaces
import IUsersRepository from '@modules/users/repositories/IUsersRepository';
import IHashProvider from '@shared/containers/providers/HashProvider/models/IHashProvider';
import ICreateUserDTO from '../dtos/ICreateUserDTO';

@injectable()
export default class CreateUserService {
  constructor(
    @inject('UsersRepository')
    private usersRepository: IUsersRepository,

    @inject('HashProvider')
    private hashProvider: IHashProvider,
  ) {}

  public async execute(data: ICreateUserDTO): Promise<User> {
    const { name, email, password, userType } = data;

    // Use findByEmail method to check if the user exists
    const checkIfUserExists = await this.usersRepository.findByEmail(email);

    // If user exists, it'll return a error
    if (checkIfUserExists) {
      throw new AppError('Email address already used.');
    }

    // Hash the password with bcrypt
    const hashedPassword = await this.hashProvider.createHash(password);

    // Create a new User in database
    const user = await this.usersRepository.create({
      name,
      email,
      password: hashedPassword, // Database will have only the hashed password
      userType,
    });

    return user;
  }
}

0 Answers0