Modules
math.ts has export on line 1, so it is a module: a file with its own scope. Names inside it stay private unless exported. Here add is exported, so other files may use it.
app.ts is a second module. Line 1 brings add in with import { add } from "./math.js". The path names math.js, the JavaScript file that will exist when the program runs, not math.ts.
Declaration files
tsc compiles math.ts with the --declaration option. As always it checks the types first; this file has no errors. Then, instead of one output file, it writes two.
The first file is math.js: the same code with every type removed. It keeps the function body, so it can run. It says nothing about which types add accepts.
The second is math.d.ts, a declaration file. declare means "this exists; its code is elsewhere". There is no body, only add's shape. tsc even wrote the return type, number, which it worked out itself.
Using a library
Ship only math.js and math.d.ts, as libraries do. Now tsc checks app.ts: it follows ./math.js, finds math.d.ts beside it and reads add's types from there. math.ts is not needed.
Line 2 calls add("2", 3). The declaration says a is a number, so tsc reports error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'. math.ts was never opened.
Change "2" to the number 2 and check again. Both arguments match the declared types, so tsc reports no errors and writes app.js: the same two lines, as they have no types to remove.
Run time
Node.js runs app.js. Its import loads math.js, the real code, and add(2, 3) prints 5. math.d.ts is never loaded: like all types, declarations are gone at run time.
What happens
;; step 1/9 · module
math.ts
> 1 export function add(a: number, b: number) {
2 return a + b;
3 }
top-level export → this file is a module
add is exported → other modules can import it
anything not exported stays inside the file
Inside one project where math.ts is still present, tsc resolves ./math.js to math.ts itself (a .ts file is found before a .d.ts of the same name). The .d.ts file is for whoever gets only the compiled files, such as the users of an npm package. If a .js file has no .d.ts beside it, strict tsc reports error TS7016: Could not find a declaration file for module. Many npm packages ship their own .d.ts files; for others, the DefinitelyTyped project publishes them as @types packages.