create a file in typescript

To create a file in TypeScript, you need to use the File System (fs) module from Node.js. Here's an example of how to create a file in TypeScript:

index.ts
import * as fs from 'fs';

// Define the file name and content
const fileName: string = 'example.txt';
const fileContent: string = 'This is an example file.';

// Create the file
fs.writeFile(fileName, fileContent, (err: NodeJS.ErrnoException) => {
  if (err) throw err;
  console.log('File created!');
});
307 chars
12 lines

In the above code, we're importing the fs module from Node.js. We then define the fileName and fileContent variables, which represent the name and content of the file we want to create. Finally, we use the writeFile method to create the file, passing in the fileName, fileContent, and a callback function that handles any errors that may occur.

When you run this code (assuming you have Node.js installed and have created a TypeScript file), it will create a file named example.txt in the current directory with the contents, "This is an example file."

related categories

gistlibby LogSnag