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.

nameof type generics

See original GitHub issue

This code currently returns “T” as result.

function myFunc<T>() {
    console.log(nameof<T>())
}
myFunc<String>()

Can we create new feature to nameof that could return name of type passed to generic function? (String in example provided)

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
dsherretcommented, Apr 16, 2019

@leolorenzoluis I think this could be done, but feel like it’s more trouble to do than it’s worth and can’t think of why it would be useful over a runtime solution. If someone were to implement this typeof function would probably need to compile the code with the compiler API in order to get the types passed in at call sites and then use that information in the transform while emitting.

I agree with @ondratra that a different design pattern would probably be better. It won’t lock all your code into needing to use it… using it could make migrating away from it difficult in the future.

1reaction
ondratracommented, Apr 16, 2019

@leolorenzoluis I think original post of @dsherret illustrate the problem well. Imho there is no way to accomplish what you are asking.

@leolorenzoluis You said you would expect number and string as results of original example, but if I create extended example you will see that it couldn’t be decided what will be nameof T.

function myNestedFunc<T>() {
    console.log(nameof<T>())
}
function myFunc<U>() {
    myNestedFunc<U>()
}
myFunc<string>()
myFunc<number>()

Even if we change current behaviour it could in then return string/number or U (and both would make sense in some situations).

You should probably find another design pattern to solve your problem.

Read more comments on GitHub >

github_iconTop Results From Across the Web

C# nameof generic type without specifying type - Stack Overflow
nameof () evaluates to a const String so it can be used in other const strings and concatenated in string literals - you...
Read more >
Please provide nameof() for generic type paremeter's properties
Collaborator. I have a string of use cases for nameof(InstanceInScope. Member) because in these cases, when you consider refactoring, what's truly important is ......
Read more >
Allow open generic type for nameof - Visual Studio Feedback
Currently if I have for example a variable of type "Dictionary<string, MyCustomType<long>>" and I want the name of its "Keys" property, I can...
Read more >
Documentation - Generics - TypeScript
A generic class has a similar shape to a generic interface. Generic classes have a generic type parameter list in angle brackets (...
Read more >
Generic Type Parameters - C# Programming Guide
In a generic type or method definition, a type parameter is a placeholder for a specific type that a client specifies when they...
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