Reading and writing files

Read project files with fs, parse JSON into a typed shape, and write temporary files under /tmp.

Runs in Zerethon IDE · TypeScript 7 Official TypeScript documentation ↗
main.ts
import * as fs from 'fs';

interface Config {
  name: string;
  retries: number;
}

const config = JSON.parse(fs.readFileSync('config.json', 'utf8')) as Config;
console.log(config.name, config.retries + 1);

fs.writeFileSync('/tmp/out.txt', `retries=${config.retries}`);
console.log(fs.readFileSync('/tmp/out.txt', 'utf8'));
config.json
{"name": "demo", "retries": 2}

Output

demo 3
retries=2
  • JSON.parse returns any; as Config tells the compiler the shape but does not check it at run time.
  • Project files are read-only; write to /tmp, which is deleted after the run.