Trying to use nominal typing for id in typeorm typescript

443 Views Asked by At

I was following this article TypeORM Best Practices using Typescript and NestJS at Libeo. I got to the point of using nominal typing in our entity ID

 @PrimaryGeneratedColumn("uuid")
 id!: string & { __brand: "userId" };

but cannot use it in find operation example

async getUserById(id: User['id']) {
const user = await this.findOne({
  where: { id: 'id' },
});
return user;

},

I am having the following errors

Type '{ id: string; }' is not assignable to type 'FindOptionsWhere<User> | FindOptionsWhere<User>[] | undefined'.
Types of property 'id' are incompatible.
    Type 'string' is not assignable to type 'boolean | FindOperator<any> | never[] | EqualOperator<never> | undefined'.
       where: { id: 'id' },

Actually don't know what am doing wrong.

but if I remove the nominal part everything works very well

@PrimaryGeneratedColumn("uuid")
id!: string

I have also tried using just Primary Column

@PrimaryColumn("uuid")
id!: string & { __brand: "userId" };

Still not working. I think it has to do with the FindOption

1

There are 1 best solutions below

0
On
enum UserIdBrand {
  __type = 'User',
}

type UserId = string & UserIdBrand;

@hittingonme Thanks

Also using enum didn't work, had to declare it as a type UserId