Nucleoid icon indicating copy to clipboard operation
Nucleoid copied to clipboard

Typescript Support

Open canpacis opened this issue 3 years ago β€’ 3 comments

At the moment typescript linting tools does not like working with nucleoid. The way I understand this is that when I register a class I get static helper methods on the class in runtime but these do not exist in the code itself, nor their type declarations.

class User {
  constructor(public name: string) {
    this.name = name;
  }
}
nucleoid.register(User);

app.get("/user", () => User.filter(() => /* */));

So something like this will throw an error like Property 'filter' does not exist on type 'typeof User'.

Are you planning to add typescript support?

canpacis avatar May 05 '22 11:05 canpacis

Definitely, I am still vetting couple options, but the best way that I can come with extending the class like this

import nucleoid, { Prototype } from "nucleoidjs";
const app = nucleoid();

class User extends Prototype {
  name: string;

  constructor(name: string) {
    super();
    this.name = name;
  }
}

app.post("/users", () => User.filter((u) => u.name === "Daphne"));

canmingir avatar May 08 '22 19:05 canmingir

The way I understand this is that when I register a class I get static helper methods on the class in runtime but these do not exist in the code itself, nor their type declarations.

That is right, the runtime rerenders JavaScript and creates graph (all of them happens at the runtime tho). It provides some shortcuts like turning class into array in order to query like in database table. Since all of them happening at the runtime, it is quite conflicts with TypeScript, which is compile time.

canmingir avatar May 08 '22 19:05 canmingir

I think an extendible prototype solution would be the cleanest. I would be easy to read and understand, while not clashing with ide's

canpacis avatar May 09 '22 14:05 canpacis