I am currently puzzled about the execution of code in files. Let's say we have a file1.ts
with the following content:
export interface myInterface {}
export function myFunction() {}
export const myConst: {}
// ... and more exports
// top-level non-exported code
if (condition) {
myConst = ...
}
And then a file2.ts
that includes one of the following:
import { myInterface } from "./file1.ts"
import * from "./file1.ts"
import * as file1 from "./file1.ts"
import "./file1.ts"
How does the behavior differ in each case? When is the top-level code in file1.ts
executed and when is it not? Is it executed even if only a specific export is imported (like in the first variant)?
This topic is driving me crazy at the moment and I have been unable to find any information about it in the TypeScript handbook section on modules.