writeFileSync
writeFileSync writes string data to a file. If the file does not exist, it is created; if it exists, it is overwritten.
Signature
writeFileSync(path: string, data: string): Result<void, FsError>tyrus
import { writeFileSync, type FsError } from "tyrus:fs";
match (writeFileSync("message.txt", "hello from tyrus")) {
Ok(_) => { print("file written"); }
Err(error) => { printError(error.code); // ERR_FS_WRITE_FAILED
}
}Error Handling
If the file cannot be written, writeFileSync returns Err(FsError) with a stable code field. Inspect the result with match or propagate it with ?:
error
tyrus
import { writeFileSync, type FsError } from "tyrus:fs";
match (writeFileSync("message.txt", "hello from tyrus")) {
Ok(_) => { print("file written"); }
Err(error) => { printError(error.code); // ERR_FS_WRITE_FAILED
}
}Copy a File
tyrus
import { readFileSync, writeFileSync, type FsError } from "tyrus:fs";
const main = (): void => {
match (readFileSync("README.md")) {
Ok(source) => {
match (writeFileSync("target/readme-copy.md", source)) {
Ok() => print("file copied");
Err(error) => printError(error.message);
}
}
Err(error) => printError(error.message);
}
};