Ming
Ming

Reputation: 1602

Typescript:could be instantiated with an arbitrary type which could be unrelated to 'import

Domain:

export interface IDepartamentProps {
  departament_name: string;
  manager_id: string;
}
export class Departament extends AggregateRoot<IDepartamentProps> {
  get DepartamentName(): string {
    return this.props.departament_name;
  }
  get ManagerID(): string {
    return this.props.manager_id;
  }
  private constructor(props: IDepartamentProps, id?: string) {
    super(props, id);
  }
  public static create = async (
    props: IDepartamentProps,
    id?: string,
  ): Promise<Departament> => {
    const guardedProps = [
      { argument: props.departament_name, argumentName: 'departament_name' },
      { argument: props.manager_id, argumentName: 'manager_id' },
    ];
    const guardResult = Guard.againstNullOrUndefinedBulk(guardedProps);
    if (!guardResult.succeeded) throw new Error(guardResult.message);
    const departament = new Departament(props, id);
    const idWasProvided = !!id;
    if (!idWasProvided) {
      departament.when(new LocationCreatedEvent(departament));
    }
    return departament;
  };
}

my mapper:

export class DepartamentMapper<Departament> implements IMapper<Departament> {
  constructor(private departamentCreator: DomainCreator<Departament>) {}
  toPersistence(t: any): Departament {
    throw new Error('Method not implemented.');
  }
  public toDomain = (raw: any): Departament => {
    if (raw instanceof Departament) return raw;
  };
}

But for some reason I am getting error when checking if the value is an instance of my Department:

if (raw instanceof Departament) return raw;

i got this:

Departament' could be instantiated with an arbitrary type which could be unrelated to

Upvotes: 0

Views: 1537

Answers (1)

Linda Paiste
Linda Paiste

Reputation: 42188

There's two problems that I see right away.

Problem

class DepartamentMapper<Departament> takes a generic with the name Department. The value of this generic has no relation to your class Department, but you've given them the same name. This is why you are getting an error in the toDomain method.

  public toDomain = (raw: any): Departament => {
    if (raw instanceof Departament) return raw;
  };

When you call raw instanceof Departament you are checking if it is an instance of the class Department. But your return type Department is declaring that it is of the generic Department, which could be anything.

Possible Solutions

If DepartmentMapper only maps Department class objects, then it should not be a generic class. Change the signature to export class DepartamentMapper implements IMapper<Departament>.

If <Department> is truly a generic, then you need to rethink how you are verifying its type.

Problem

In this same function you have another error which is the return type. You return raw when you have verified that it is a Department, but you do nothing if it's not. So you get error 2366 "Function lacks ending return statement and return type does not include 'undefined'".

Possible Solutions

  1. change the return type to Department | undefined
  2. throw an Error if raw is not a Department
  3. make this into a guard function, which is basically a boolean that narrows the type of the input when true. It's kind of trivially simple in this case and doesn't depend on the instance so it could be static. But here's what such a method would look like:
public isDepartment = (raw: any): raw is Departament => {
    return raw instanceof Departament;
};

Upvotes: 1

Related Questions