question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add type-safe method to access model class by instance

See original GitHub issue

Problem

Given an arbitrary model instance, I would like to know which specific model this instance belongs to.

Example use case: in a Model.beforeInsert global hook, I would like to recognize each of inputItems’s model class (e.g. look up its tableName).

I can already access it with item.constructor but it’s typed as Function so I have to use ugly casts like:

if (item instanceof Model) {
  console.log((item.constructor as ModelClass<Model>).tableName)
}

Proposed API addition

There could be an instance level getter item.$modelClass which would simply return this.constructor properly typed.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
koskimascommented, Jan 1, 2021

Actually that’s what the query builder’s modelClass() method also returns. I’ll add the getter.

0reactions
koskimascommented, Jan 1, 2021

There’s no way to add a method, getter or a property that has the type typeof this, which would be the correct type of this.constructor. Typescript simply doesn’t allow that. We could add $modelClass: ModelClass<this> but that will only contain the static interface of the Model base class and none of the static methods defined for the subclass. Adding such property feels like a hack.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I get a class instance of generic type T? - Stack Overflow
In a method of Foo , I want to get the class instance of type T , but I just can't call T.class...
Read more >
Java Generics Example Tutorial - Generic Method, Class ...
Java Generic Class. We can define our own classes with generics type. A generic type is a class or interface that is parameterized...
Read more >
Generic classes and methods | Microsoft Learn
Learn about generics. Generic types maximize code reuse, type safety, and performance, and are commonly used to create collection classes.
Read more >
Documentation - Generics - TypeScript
As we cover in our section on classes, a class has two sides to its type: the static side and the instance side....
Read more >
Object Type Casting in Java - Baeldung
What if we want to use the variable of type Animal to invoke a method available only to Cat class? Here comes the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found