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

Disallow template literals on colors and modifiers in the TypeScript types #380

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
111 changes: 62 additions & 49 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,15 @@ declare namespace chalk {
}

interface ChalkFunction {
/**
Use a string.

@remarks Template literals are unsupported for nested calls (see [issue #341](https://github.com/chalk/chalk/issues/341))
*/
(text: string): string;
}

interface ChalkTemplateFunction {
/**
Use a template string.

Expand All @@ -153,7 +162,7 @@ declare namespace chalk {
(...text: unknown[]): string;
}

interface Chalk extends ChalkFunction {
interface ChalkObject {
/**
Return a new Chalk instance.
*/
Expand Down Expand Up @@ -291,105 +300,109 @@ declare namespace chalk {
/**
Modifier: Resets the current color chain.
*/
readonly reset: Chalk;
readonly reset: ChalkProperty;

/**
Modifier: Make text bold.
*/
readonly bold: Chalk;
readonly bold: ChalkProperty;

/**
Modifier: Emitting only a small amount of light.
*/
readonly dim: Chalk;
readonly dim: ChalkProperty;

/**
Modifier: Make text italic. (Not widely supported)
*/
readonly italic: Chalk;
readonly italic: ChalkProperty;

/**
Modifier: Make text underline. (Not widely supported)
*/
readonly underline: Chalk;
readonly underline: ChalkProperty;

/**
Modifier: Inverse background and foreground colors.
*/
readonly inverse: Chalk;
readonly inverse: ChalkProperty;

/**
Modifier: Prints the text, but makes it invisible.
*/
readonly hidden: Chalk;
readonly hidden: ChalkProperty;

/**
Modifier: Puts a horizontal line through the center of the text. (Not widely supported)
*/
readonly strikethrough: Chalk;
readonly strikethrough: ChalkProperty;

/**
Modifier: Prints the text only when Chalk has a color support level > 0.
Can be useful for things that are purely cosmetic.
*/
readonly visible: Chalk;
readonly visible: ChalkProperty;

readonly black: Chalk;
readonly red: Chalk;
readonly green: Chalk;
readonly yellow: Chalk;
readonly blue: Chalk;
readonly magenta: Chalk;
readonly cyan: Chalk;
readonly white: Chalk;
readonly black: ChalkProperty;
readonly red: ChalkProperty;
readonly green: ChalkProperty;
readonly yellow: ChalkProperty;
readonly blue: ChalkProperty;
readonly magenta: ChalkProperty;
readonly cyan: ChalkProperty;
readonly white: ChalkProperty;

/*
Alias for `blackBright`.
*/
readonly gray: Chalk;
readonly gray: ChalkProperty;

/*
Alias for `blackBright`.
*/
readonly grey: Chalk;

readonly blackBright: Chalk;
readonly redBright: Chalk;
readonly greenBright: Chalk;
readonly yellowBright: Chalk;
readonly blueBright: Chalk;
readonly magentaBright: Chalk;
readonly cyanBright: Chalk;
readonly whiteBright: Chalk;

readonly bgBlack: Chalk;
readonly bgRed: Chalk;
readonly bgGreen: Chalk;
readonly bgYellow: Chalk;
readonly bgBlue: Chalk;
readonly bgMagenta: Chalk;
readonly bgCyan: Chalk;
readonly bgWhite: Chalk;
readonly grey: ChalkProperty;

readonly blackBright: ChalkProperty;
readonly redBright: ChalkProperty;
readonly greenBright: ChalkProperty;
readonly yellowBright: ChalkProperty;
readonly blueBright: ChalkProperty;
readonly magentaBright: ChalkProperty;
readonly cyanBright: ChalkProperty;
readonly whiteBright: ChalkProperty;

readonly bgBlack: ChalkProperty;
readonly bgRed: ChalkProperty;
readonly bgGreen: ChalkProperty;
readonly bgYellow: ChalkProperty;
readonly bgBlue: ChalkProperty;
readonly bgMagenta: ChalkProperty;
readonly bgCyan: ChalkProperty;
readonly bgWhite: ChalkProperty;

/*
Alias for `bgBlackBright`.
*/
readonly bgGray: Chalk;
readonly bgGray: ChalkProperty;

/*
Alias for `bgBlackBright`.
*/
readonly bgGrey: Chalk;
readonly bgGrey: ChalkProperty;

readonly bgBlackBright: Chalk;
readonly bgRedBright: Chalk;
readonly bgGreenBright: Chalk;
readonly bgYellowBright: Chalk;
readonly bgBlueBright: Chalk;
readonly bgMagentaBright: Chalk;
readonly bgCyanBright: Chalk;
readonly bgWhiteBright: Chalk;
readonly bgBlackBright: ChalkProperty;
readonly bgRedBright: ChalkProperty;
readonly bgGreenBright: ChalkProperty;
readonly bgYellowBright: ChalkProperty;
readonly bgBlueBright: ChalkProperty;
readonly bgMagentaBright: ChalkProperty;
readonly bgCyanBright: ChalkProperty;
readonly bgWhiteBright: ChalkProperty;
}

interface ChalkProperty extends ChalkObject, ChalkFunction {}

interface Chalk extends ChalkObject, ChalkFunction, ChalkTemplateFunction {}
}

/**
Expand All @@ -398,7 +411,7 @@ Call the last one as a method with a string argument.
Order doesn't matter, and later styles take precedent in case of a conflict.
This simply means that `chalk.red.yellow.green` is equivalent to `chalk.green`.
*/
declare const chalk: chalk.Chalk & chalk.ChalkFunction & {
declare const chalk: chalk.Chalk & chalk.ChalkFunction & chalk.ChalkTemplateFunction & {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't & chalk.ChalkFunction & chalk.ChalkTemplateFunction moot here as chalk.Chalk already extend those?

supportsColor: chalk.ColorSupport | false;
Level: typeof LevelEnum;
Color: Color;
Expand Down
90 changes: 47 additions & 43 deletions index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ const name = 'John';
expectType<string>(chalk`Hello {bold.red ${name}}`);
expectType<string>(chalk`Works with numbers {bold.red ${1}}`);

// -- Members of the Chalk interface do not support template literals (#341) --
expectError(chalk.bold`Hello {bold.red ${name}}`);
expectError(chalk.bold`Works with numbers {bold.red ${1}}`);

// -- Color methods --
expectType<colorReturn>(chalk.hex('#DEADED'));
expectType<colorReturn>(chalk.keyword('orange'));
Expand Down Expand Up @@ -70,15 +74,15 @@ expectType<string>(chalk.inverse('foo'));
expectType<string>(chalk.hidden('foo'));
expectType<string>(chalk.strikethrough('foo'));
expectType<string>(chalk.visible('foo'));
expectType<string>(chalk.reset`foo`);
expectType<string>(chalk.bold`foo`);
expectType<string>(chalk.dim`foo`);
expectType<string>(chalk.italic`foo`);
expectType<string>(chalk.underline`foo`);
expectType<string>(chalk.inverse`foo`);
expectType<string>(chalk.hidden`foo`);
expectType<string>(chalk.strikethrough`foo`);
expectType<string>(chalk.visible`foo`);
expectError(chalk.reset`foo`);
expectError(chalk.bold`foo`);
expectError(chalk.dim`foo`);
expectError(chalk.italic`foo`);
expectError(chalk.underline`foo`);
expectError(chalk.inverse`foo`);
expectError(chalk.hidden`foo`);
expectError(chalk.strikethrough`foo`);
expectError(chalk.visible`foo`);

// -- Colors --
expectType<string>(chalk.black('foo'));
Expand Down Expand Up @@ -115,40 +119,40 @@ expectType<string>(chalk.bgBlueBright('foo'));
expectType<string>(chalk.bgMagentaBright('foo'));
expectType<string>(chalk.bgCyanBright('foo'));
expectType<string>(chalk.bgWhiteBright('foo'));
expectType<string>(chalk.black`foo`);
expectType<string>(chalk.red`foo`);
expectType<string>(chalk.green`foo`);
expectType<string>(chalk.yellow`foo`);
expectType<string>(chalk.blue`foo`);
expectType<string>(chalk.magenta`foo`);
expectType<string>(chalk.cyan`foo`);
expectType<string>(chalk.white`foo`);
expectType<string>(chalk.gray`foo`);
expectType<string>(chalk.grey`foo`);
expectType<string>(chalk.blackBright`foo`);
expectType<string>(chalk.redBright`foo`);
expectType<string>(chalk.greenBright`foo`);
expectType<string>(chalk.yellowBright`foo`);
expectType<string>(chalk.blueBright`foo`);
expectType<string>(chalk.magentaBright`foo`);
expectType<string>(chalk.cyanBright`foo`);
expectType<string>(chalk.whiteBright`foo`);
expectType<string>(chalk.bgBlack`foo`);
expectType<string>(chalk.bgRed`foo`);
expectType<string>(chalk.bgGreen`foo`);
expectType<string>(chalk.bgYellow`foo`);
expectType<string>(chalk.bgBlue`foo`);
expectType<string>(chalk.bgMagenta`foo`);
expectType<string>(chalk.bgCyan`foo`);
expectType<string>(chalk.bgWhite`foo`);
expectType<string>(chalk.bgBlackBright`foo`);
expectType<string>(chalk.bgRedBright`foo`);
expectType<string>(chalk.bgGreenBright`foo`);
expectType<string>(chalk.bgYellowBright`foo`);
expectType<string>(chalk.bgBlueBright`foo`);
expectType<string>(chalk.bgMagentaBright`foo`);
expectType<string>(chalk.bgCyanBright`foo`);
expectType<string>(chalk.bgWhiteBright`foo`);
expectError(chalk.black`foo`);
expectError(chalk.red`foo`);
expectError(chalk.green`foo`);
expectError(chalk.yellow`foo`);
expectError(chalk.blue`foo`);
expectError(chalk.magenta`foo`);
expectError(chalk.cyan`foo`);
expectError(chalk.white`foo`);
expectError(chalk.gray`foo`);
expectError(chalk.grey`foo`);
expectError(chalk.blackBright`foo`);
expectError(chalk.redBright`foo`);
expectError(chalk.greenBright`foo`);
expectError(chalk.yellowBright`foo`);
expectError(chalk.blueBright`foo`);
expectError(chalk.magentaBright`foo`);
expectError(chalk.cyanBright`foo`);
expectError(chalk.whiteBright`foo`);
expectError(chalk.bgBlack`foo`);
expectError(chalk.bgRed`foo`);
expectError(chalk.bgGreen`foo`);
expectError(chalk.bgYellow`foo`);
expectError(chalk.bgBlue`foo`);
expectError(chalk.bgMagenta`foo`);
expectError(chalk.bgCyan`foo`);
expectError(chalk.bgWhite`foo`);
expectError(chalk.bgBlackBright`foo`);
expectError(chalk.bgRedBright`foo`);
expectError(chalk.bgGreenBright`foo`);
expectError(chalk.bgYellowBright`foo`);
expectError(chalk.bgBlueBright`foo`);
expectError(chalk.bgMagentaBright`foo`);
expectError(chalk.bgCyanBright`foo`);
expectError(chalk.bgWhiteBright`foo`);

// -- Complex --
expectType<string>(chalk.red.bgGreen.underline('foo'));
Expand Down