1

I'm trying to create a process that looks at a bunch of *.ts files in a folder and determines which ones contain a class that implements a specific interface. While I'd like to be able to include multiple classes in one file, I'm fine if I can only do one.

At present, I'm simply loading the files, calling the constructor and then checking for the existence of a few properties, but some of these files may contain enums which are throwing exceptions.

I know interfaces don't have a runtime equivalent so I can't do a type check against them, but is there another option to check if what's in a file is a class / enum?

EDIT: The scenario is that I have an interface that defines an Express.js route. I then have a single class that looks at all the files in a folder and will load each one. For each file, I want to see "if it is of type IRouteDefinition then dynamically load its route & handler"

5
  • Can you provide a little bit more detail, e.g. why you want to check the type of a class? If you only want to do some static analysis it would suggest using the TS language service. Commented Aug 18, 2016 at 21:19
  • Maybe I didn't describe it right... the language service is not what I want to use... this needs to happen at runtime.The scenario is that I have an interface that defines an Express.js route. I then have a single class that looks at all the files in a folder and will load each one. For each file, I want to see "if it is of type IRouteDefinition then dynamically load it's route & handler" Commented Aug 19, 2016 at 10:20
  • Yeah, but as you already said: interfaces do not exist at runtime. So I guess you have to use the language service at runtime. Or a completely other approach would be to "tag" such route definitions via decorators. Commented Aug 19, 2016 at 11:18
  • Oh... that's interesting... decorating the class like we do in ng2. Any pointers on where to read up on creating custom decorators? Commented Aug 19, 2016 at 20:50
  • Added an answer :) Commented Aug 19, 2016 at 21:09

2 Answers 2

2

I know interfaces don't have a runtime equivalent so I can't do a type check against them, but is there an other option to check if what's in a file is a class / enum

This sort of thing is what the language service is for. However the learning curve is a bit steep (but totally worth it!)

Docs

Sign up to request clarification or add additional context in comments.

Comments

1

As mentioned in one of my comments, you could use decorators to do this. The TypeScript Docs got you covered on how to create a custom decorator: https://www.typescriptlang.org/docs/handbook/decorators.html

Here is an example implementation to get you started: http://jsbin.com/voyemu/3/edit?js,console

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.