Location of declaration #88
Answered
by
Hookyns
carloszimm
asked this question in
Q&A
-
Is there any way to inquiry a type (classes, functions, etc) about the file where they are declared or it is only achievable through a parser for instance? |
Beta Was this translation helpful? Give feedback.
Answered by
Hookyns
Jul 19, 2023
Replies: 1 comment 3 replies
-
Hi, Is this something you want? import { getType, Type, AccessModifier, Accessor } from 'tst-reflect';
function logTypeDescription<T>() {
console.log(getType<T>().constructorDescription);
}
export class SomeClass {
property1: string = 'p1';
property2: string = 'p2';
property3: number = 3;
get property4(): 'foo' | 'bar' {
return 'foo';
}
constructor(a: string) {}
method1(): void {}
}
logTypeDescription<SomeClass>();
// Get Ctor of the type, create instance and log value of "property4"
getType<SomeClass>()
.getCtor()
.then((SomeClassCtor) => {
console.log(new SomeClassCtor().property4);
}); output:
StackBlitz demo. |
Beta Was this translation helpful? Give feedback.
3 replies
Answer selected by
carloszimm
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
if getting the constructor by
getCtor()
is not enough, you can enable "server" mode if you want to know exact file locations. Then you can usetype.constructorDescription
property. See configuration.Is this something you want?