Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document a way to determine class at runtime (reflection) #3660

Closed
drudru opened this issue Jun 27, 2015 · 3 comments
Closed

Document a way to determine class at runtime (reflection) #3660

drudru opened this issue Jun 27, 2015 · 3 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@drudru
Copy link

drudru commented Jun 27, 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.

@DanielRosenwasser
Copy link
Member

Thanks @drudru, I've filed microsoft/TypeScript-Handbook#52, not sure if that's exactly what you had in mind.

@mhegazy
Copy link
Contributor

mhegazy commented Jun 29, 2015

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 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
@mhegazy mhegazy closed this as completed Jun 29, 2015
@drudru
Copy link
Author

drudru commented Jul 3, 2015

Ok - thx

@DanielRosenwasser - thanks for filing that to the Handbook

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

3 participants