find the extension of a file in typescript

To find the extension of a file in TypeScript, you can use the path module from Node.js. Here's an example:

index.ts
import path from 'path';

const filename = 'example.txt';
const extension = path.extname(filename);

console.log(extension); // Output: .txt
141 chars
7 lines

In this example, we first import the path module using the import statement. Then, we declare a variable called filename and assign it the value of the file name, including its extension. We then use the path.extname() method to extract the extension from the file name, and assign it to the extension variable.

Finally, we log the value of extension to the console to verify that the extension was successfully extracted.

Note that the path module's extname() method returns the extension including the dot (.) character. If you want to remove the dot, you can use the substring() method like this:

index.ts
const extensionWithoutDot = extension.substring(1);
52 chars
2 lines

gistlibby LogSnag