You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Typescript generates standard JavaScript and erases the types in the generated code. I think this is fine, but for many dynamic programs, there is a need to process a heterogeneous collection. In that situation, the ability to easily determine the class of an instance at run-time is a requirement.
A solution or best practice for his does not appear to be specified or described in the Handbook or Specification. When I search on the net with the phrase "typescript reflection class name", the top answers refer to a technique that uses a regexp on the string form of the function definition. That is not a good solution.
When I search on "ES6 class name", there is the method of using the instance.constructor.name == 'ClassName' technique. A quick test in the console showed that this worked for me, but I didn't consider testing all of the cases. I find that a little verbose, but it may be part of the standard. Also, completion in the editor may make this a non issue.
I think the official documentation should cover this topic and the current approved way of determining class at run-time. I would contribute patches to the docs, but I'm not 100% sure what the right answer is.
The text was updated successfully, but these errors were encountered:
Classes are compiled to functions. if you are in a engine that supports function.name iinstance.constructor.name should work. if you are on an older browser, you can use a pollyfill for function.name, e.g. corejs, (implementation here: https://github.com/zloirock/core-js/blob/master/modules/es6.function.name.js).
Please note that this is a Javascript feature, that was added in the ES6/ES2015 and TypeScript has no part in supporting this.
mhegazy
added
Question
An issue which isn't directly actionable in code
API
Relates to the public API for TypeScript
and removed
API
Relates to the public API for TypeScript
labels
Jun 29, 2015
Typescript generates standard JavaScript and erases the types in the generated code. I think this is fine, but for many dynamic programs, there is a need to process a heterogeneous collection. In that situation, the ability to easily determine the class of an instance at run-time is a requirement.
A solution or best practice for his does not appear to be specified or described in the Handbook or Specification. When I search on the net with the phrase "typescript reflection class name", the top answers refer to a technique that uses a regexp on the string form of the function definition. That is not a good solution.
When I search on "ES6 class name", there is the method of using the instance.constructor.name == 'ClassName' technique. A quick test in the console showed that this worked for me, but I didn't consider testing all of the cases. I find that a little verbose, but it may be part of the standard. Also, completion in the editor may make this a non issue.
I think the official documentation should cover this topic and the current approved way of determining class at run-time. I would contribute patches to the docs, but I'm not 100% sure what the right answer is.
The text was updated successfully, but these errors were encountered: