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.

Unable to get type arguments of class

See original GitHub issue

It looks like reading the type arguments of a class doesn’t work. Inner Type of type aliases resolve correctly, but not classes.

type Foo<Type> = Type & {}

class Bar<Type> {}

@inject()
export class Baz {
  foo: Foo<string>   // type.innerTypes contains string

  bar: Bar<string>   // type.innerTypes is empty. type.getTypeParameters() returns Type. type.getTypeArguments() is empty
}

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
Hookynscommented, Aug 6, 2022

@avin-kavish

  1. You can. https://github.com/Hookyns/tst-reflect/blob/main/tests/src/11-generic-class-decorator.ts @classDecorator
  2. Now all the types passed to getType<T>() and types decorated by decorators marked by @reflect are “reflected”.
    Next version is prepared for generating metadata about all the modules and types.
    It’s in progress and the latest progress is private until publish of final version.
0reactions
avin-kavishcommented, Aug 5, 2022

that’s great!

I was considering the following about tst-reflect usage,

  1. Can’t we use a decorator directly, without having to call a method that returns a decorator?
/**
 * @reflect
 */
function inject<TType extends Constructor>(ctor: TType) {
    const typeInfo = getType<TType>()
}

// usage
@inject
class Foo {}
  1. Can’t we collect type info for all classes/interfaces in a project without using decorators? Maybe it’s too slow?
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
A popular solution to this is to pass the Class of the type parameter into the constructor of the generic type, e.g. class...
Read more >
Restrictions on Generics (The Java™ Tutorials > Learning the ...
A class's static field is a class-level variable shared by all non-static objects of the class. Hence, static fields of type parameters are...
Read more >
Constraints on type parameters - C# Programming Guide
Learn about constraints on type parameters. Constraints tell the compiler what capabilities a type argument must have.
Read more >
Java Generics Example Tutorial - Generic Method, Class ...
We use angle brackets (<>) to specify the type parameter. To understand the benefit, let's say we have a simple class as:
Read more >
[Solved] Get generic type name in java - CodeProject
I got an exception that says: " class sun.reflect.generics.reflectiveObjects.TypeVariableImpl cannot be cast to class java.lang.Class (sun.
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