Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type '(filenames: string[]) => Commands | Promise<Commands>' is not assignable to type 'string' #1376

Closed
niebag opened this issue Dec 14, 2023 · 2 comments

Comments

@niebag
Copy link

niebag commented Dec 14, 2023

Description

I believe my config should work. According to the documentation, I should be able to have an array of functions assigned to a matcher e.g. '*.{ts,tsx,cjs,js}'. It seems the current lint-staged types don't allow this.

lint-staged.config.js

import path from "path";

/** @type {import("lint-staged").ConfigFn} */
function lint(filenames) {
    if (filenames.length > 10) return 'next lint --fix';

    return `next lint --fix --file ${filenames.map((f) => path.relative(process.cwd(), f)).join(' --file ')}`;
}

/** @type {import("lint-staged").ConfigFn} */
function prettier(filenames) {
    if (filenames.length > 10) return 'prettier --write';

    return `prettier --write ${filenames.map((f) => path.relative(process.cwd(), f)).join(' ')}`;
}

/** @type {import("lint-staged").Config} */
const config = {
    '*.{ts,tsx,cjs,js}': [lint], // Type '(filenames: string[]) => Commands | Promise<Commands>' is not assignable to type 'string'
    // '*.{ts,tsx,cjs,js}': lint // This works
};

export default config;

index.d.ts

export type Commands = string[] | string;

export type ConfigFn = (filenames: string[]) => Commands | Promise<Commands>;

export type Config = ConfigFn | { [key: string]: Commands | ConfigFn };

As shown, this only allows a string, an array of strings or a single function in { [key: string]: Commands | ConfigFn }.

I think this could easily fixed by changing

export type Config = ConfigFn | { [key: string]: Commands | ConfigFn };

to

export type Config = ConfigFn | { [key: string]: Commands | ConfigFn | ConfigFn[] };

Steps to reproduce

  1. bun add --dev lint-staged @types/lint-staged
  2. Make file called lint-staged.config.js
  3. Create a config as shown in this issue's Description

Environment

  • OS: MacOS Sonoma 14.1
  • Node.js: v18.18.0
  • lint-staged: ^15.2.0
  • @types/lint-staged: ^13.2.2
@iiroj
Copy link
Member

iiroj commented Dec 14, 2023

The @types/lint-staged package is not officially supported, so I can't really comment on it.

@iiroj iiroj closed this as completed Dec 14, 2023
@iiroj
Copy link
Member

iiroj commented Dec 14, 2023

Closing in place of #1359

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants