npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cozy-tsconfig

v1.2.0

Published

Shared TypeScript config for Cozy Cloud projects

Downloads

528

Readme

Shared TypeScript config for Cozy projects.

🖥️ Install

yarn add --dev cozy-tsconfig

This config requires TypeScript 4.5 or later

📦 What's included by default

  • [x] strict mode is enabled.
  • [x] Only emit declaration files, no transpiling, you need Babel or another transpiler to do that.
  • [x] JavaScript files are not ignored, meaning automatic declaration files will be emitted for them.
  • [x] jsx is supported and set to react.

🚀 Usage

Typechecking in the IDE

The basic usage is to extend the configuration file to have Typechecking in your IDE.

To use it, create a tsconfig.json file in the root of your project and extend the configuration. Please note that you have to provide paths to your source and output directories, as it can not be provided by the configuration file itself.

Here is an example where your work is in src and test and your output is in dist. Obviously, you don't want to typecheck dist files so you ignore them alongside node_modules or anything else unwanted.

{
  "extends": "cozy-tsconfig",
  "include": ["src/**/*", "test/**/*"],
  "exclude": [
    "node_modules",
    "dist"
  ]
}

Typechecking in the CI/CD pipeline (optional)

If you want to use TypeScript in your CI/CD pipeline (for instance if you just run linter and tests without building yet), you can use the following script to check your codebase:

{
  "scripts": {
    "typecheck": "yarn tsc --noEmit"
  }
}

Emit declaration files (useful for libraries)

To emit declaration files, create a new file in the root of your project, for instance tsconfig-build.json.

By default, this configuration will emit declaration files in the dist directory, but you can overwrite it by providing a declarationDir option in the compilerOptions field.

This file will be used to emit declarations, it will extend the base config but only include the files you want to actually emit declarations for.

Here is a basic example of what you can do:

{
  "extends": "./tsconfig.json",
  "include": [
    "src/**/*",
  ],
  "exclude": [
    "**/*.spec.js",
    "**/*.spec.jsx",
    "**/*.spec.ts",
    "**/*.spec.tsx",
    "**/*.test.js"
    "**/*.test.jsx",
    "**/*.test.ts",
    "**/*.test.tsx",
    "tests"
  ],
  "compilerOptions": {
    "declarationDir": "build" // If your lib directory is not named `dist`, you can overwrite it here
  }
}

Following the example above where we typechecked our tests, we now only emit declaration files for the src directory, and we ignore all test files. We don't want to emit declaration files for our tests, as they are not part of the public API of our library.

Then, you can use the following scripts to build your project and your declarations in sequence:

{
  "scripts": {
    "build": "yarn build:js && yarn build:types",
    "build:js": "babel ./src -d ./dist",
    "build:types": "yarn tsc -p tsconfig-build.json"
  }
}

This will use the build config to emit declaration files (the build config ignores test files and won't emit declaration files for them). As mentionned earlier, TypeScript will not transpile your code, so you need to use another tool to do that. In this example, we used Babel to transpile our code.

Do not forget to update your package.json accordingly. You need to point to the dist directory for the main field, and to the dist/index.d.ts file for the types field (assuming your build directory is called dist).

{
  "name": "my-cozy-package",
  "version": "1.0.0",
  "main": "dist/index.js",
  "types": "dist/index.d.ts"
}

Overriding the configuration

If the provided configuration file don't exactly fit your needs, you can override it.

For example, if you want to disable strict mode, you can do:

{
  "extends": "cozy-tsconfig",
  "compilerOptions": {
    "strict": false
  }
}

Please refer to the TypeScript documentation for more information.

💡 Recommendations

  • Keep eslint-config-cozy-app as up-to-date as possible. While it's not a direct dependency of this package, it's recommended to use it to keep your codebase consistent with the rest of the Cozy ecosystem. It will add a lot of TypeScript checks to your codebase that aren't handled by TypeScript itself.

  • Don't hesitate to start adding TypeScript files to a 100% JavaScript project. It's a great way to start using TypeScript without having to rewrite everything at once. You can also start by adding TypeScript files to your tests, as they are not part of the public API of your library and won't be transpiled.

  • If you struggle with typings errors, you can use the @ts-expect-error, @ts-ignore or even @ts-nocheck directives to ignore them on a line/file basis. Please refer to the TypeScript documentation for more information. Alternatively, you can use the @ts-check directive to enable typechecking for a file, and disable it for the rest of the project by adding "checkJs": false to your tsconfig.json file. Ultimately, try to fix the errors, as they are there for a reason, but practically, each project will have its own needs so it's up to you to decide what's best for it.

  • For pure TypeScript files, it is recommended to fix every type error and stick to strict mode, otherwise it defeats the purpose of using TypeScript in the first place.

📝 License

Copyright © 2023 Cozy.io.

This project is MIT licensed.