Currently, I am exploring the functionality of the Typescript Compiler API along with the local file system to access the exported object from a typescript config file and integrate it into node.js.
Consider the following simple example:
// sample.config.ts
type Config = {
hello: string;
};
const config: Config = {
hello: "world",
};
export default config;
In a separate file, how can I utilize the Compiler API to extract the exported object and assign it to a variable for use in js?
//another-file.js
const source = "./sample.config.ts"
let exportedObject = /* code using Compiler API function(s) to fetch exported object from 'sample.config.ts' */
console.log(exportedObject.hello)
// outputs "world"
I have managed to load a program and source file, but I need guidance on what steps to take next. Any recommended documentation or resources are highly appreciated!
//another-file.js
const source = "./sample.config.ts";
const program = ts.createProgram([source]);
const sourceFile = program.getSourceFile(source);