0

I need to find path to the file from which the class/interface is exported. Let's say i have a file builder.ts inside ./src folder with following content:

export class Builder {}

And i have index.ts in current directory, with following content:

import { Builder } from "./src/builder";

Later, i might need to know the path to the file, from which Builder class was imported, so i want to have some kind of function, that would take a class/interface/ and return the path to the file where the entity is defined. So i could write something like this in the index.ts file

const path = getPath(Builder)

And path variable should equal to "./src/builder". Is it possible to have this kind of function?

8
  • You already have the path? put the string into a variable Commented Jun 30, 2022 at 12:57
  • Hi. I need to have that done automatically. Commented Jun 30, 2022 at 12:58
  • Please share a more accurate minimal reproducible example? Commented Jun 30, 2022 at 13:04
  • Thanks for your time. I shared more detailed example. Commented Jun 30, 2022 at 13:51
  • Really, your best bet is to create sub-types and follow OOP principles. TypeScript can pick up the type and branch the logic for you. Commented Jun 30, 2022 at 13:54

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.