Search code examples
typescriptio

How come typescript writes to file without open/close


All languages I know of do the open-write-close routine. But in typescript:

import { writeFileSync } from "fs";
writeFileSync(filename, "hello")

seems to work fine. How does this "magic" work?


Solution

  • In the class of FileHandle contain the fs module that describe in this link:

    File system module

    Class: FileHandle

    A object is an object wrapper for a numeric file descriptor.

    Instances of the object are created by the fsPromises.open() method.

    All objects are s.

    If a is not closed using the filehandle.close() method, it will try to automatically close the file descriptor and emit a process warning, helping to prevent memory leaks. Please do not rely on this behavior because it can be unreliable and the file may not be closed. Instead, always explicitly close s. Node.js may change this behavior in the future.