nodejs-datastore icon indicating copy to clipboard operation
nodejs-datastore copied to clipboard

Add support for generic types

Open Zertz opened this issue 5 years ago • 0 comments

Is your feature request related to a problem? Please describe.

While the interfaces are well typed, we have to resort to casting results when generics could provide an elegant solution.

Describe the solution you'd like

type User = {
  name: string;
};

// get
const user = await datastore.get<User>(key);

user.hello; // Property 'hello' does not exist on type 'User'

// get with multiple keys of the same type
const users = await datastore.get<User[]>(keys);

// get with multiple keys of various types
const [user, dog, cat] = await datastore.get<[User, Dog, Cat]>(keys);

// runQuery
const [users] = datastore.runQuery<User>(query);

users.forEach((user) => {
  user.hello; // Property 'hello' does not exist on type 'User'
});

// save
await datastore.save<User>({
  key,
  data: {
    hello: "world" // Object literal may only specify known properties, and 'hello' does not exist in type 'User'
  }
});

Describe alternatives you've considered

Not any that I'm aware of!

Additional context

Not much to add!

Zertz avatar Jun 25 '20 23:06 Zertz