redis / redis-om-node

Object mapping, and more, for Redis and Node.js. Written in TypeScript.
MIT License
1.14k stars 77 forks source link

How to get type inference? #223

Open vittis opened 6 months ago

vittis commented 6 months ago

I might be missing something trivial, but here's my case:

// define schema
const roomSchema = new Schema("room", {
  id: { type: "string" },
  name: { type: "string" },
  members: { type: "string[]" },
});

// somewhere in code...
const room = await roomRepository.fetch(roomId);
if (room.members.includes(session.userId)) { // ts-error here 
    // ...
}

The error is:

Property 'includes' does not exist on type 'string | number | true | Date | EntityData | Point | (EntityData | EntityDataValue)[] | (EntityData | EntityDataValue)[]'.

That is because looks like there's no type inference. When I type "room." the properties doesn't show up. I tried creating a interface for Room and tried plugging into generics or casting but nothing worked...

I could check if room.members exists and then check if its type is array, is that what I'm supposed to do?

MR4online commented 6 months ago

you have to do this by yourself.

...fetch(roomId) only has the Return-Type Entity, without knowing your action model properties.

you may define a Type:

export type Room = {
id: string,
name: string,
members: string[]
};

and then use it like:

const room = <Room> await roomRepository.fetch(roomId);
shirecoding commented 5 months ago

Wish there was a better way? other than redefining

arshia-gh commented 4 months ago

This

you have to do this by yourself.

...fetch(roomId) only has the Return-Type Entity, without knowing your action model properties.

you may define a Type:

export type Room = {
id: string,
name: string,
members: string[]
};

and then use it like:

const room = <Room> await roomRepository.fetch(roomId);

I would much prefer if this was inferred from the schema when creating a repository like many other ORMs. It'd result in way fewer bugs and improve DX

MYKEU commented 4 months ago

💯 - this would be very useful to have

shirecoding commented 4 months ago

yea i end up having to define so many types eg. entity type, return from rest api type etc..

how can the typed repository fetch not even return the correct type? this is not even even a typescript library

shirecoding commented 4 months ago

This

you have to do this by yourself. ...fetch(roomId) only has the Return-Type Entity, without knowing your action model properties. you may define a Type:

export type Room = {
id: string,
name: string,
members: string[]
};

and then use it like:

const room = <Room> await roomRepository.fetch(roomId);

I would much prefer if this was inferred from the schema when creating a repository like many other ORMs. It'd result in way fewer bugs and improve DX

this doesnt work though it needs to export type Room extends Entity else it complains there is insufficient overlap

hanayashiki commented 2 months ago

this is a fake ts library