nameof type generics
See original GitHub issueThis 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:
- Created 5 years ago
- Comments:11 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@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.
@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
andstring
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.Even if we change current behaviour it could in then return
string/number
orU
(and both would make sense in some situations).You should probably find another design pattern to solve your problem.