I am trying to use the Typescript Compiler API and local file system to retrieve the exported object of a typescript config file and use it in node.js.
Given a simplified example like the below:
// test.config.ts
type Config = {
hello: string;
};
const config: Config = {
hello: "world",
};
export default config;
In another file how would I use the Compiler API to extract the exported object into a variable so I can use it in js?
//another-file.js
const source = "./test.config.ts"
let exportedObject = /* some Compiler API function(s) to retrieve exported object from 'test.config.ts' */
console.log(exportedObject.hello)
// logs "world"
I've been able to load a program and source file - but I'm a bit lost on what do do next. Any documentation/resources would be greatly appreciated!
//another-file.js
const source = "./test.config.ts";
const program = ts.createProgram([source]);
const sourceFile = program.getSourceFile(source);