Search code examples
node.jstypescriptprettierprettier-eslint

How to configure prettier to check all files with a specific extension


I have installed prettier via

yarn add prettier

I would like prettier to only format typescript code (I am developing AWS CDK project, there is no src folder by convention & there could be typescript files here and there). In other words, I would like prettier to check all files in my project that has extension *.ts.

I checked its documentation for configuration. But there is no such option to specify file extension.

How can I run prettier for only *ts files then? Is it even possible? If not, what could be the workaround?


Solution

  • To exclude files from formatting, create a .prettierignore file at the root of your project.

    And to format only the *.ts files you should ignore everything but the *.ts files.

    Example

    # Ignore everything recursively
    *
    
    # But not the .ts files
    !*.ts
    
    # Check subdirectories too
    !*/
    

    In the code above, the * means to ignore everything including the subfolders, and the next line !*.ts tells the prettier to reverse the previous ignoring of the .ts files. The last line !*/ means to check the subdirectories too, but with the previous rule, it's only looking for the .ts files.

    Check the prettier and gitignore docs for more information.