Skip to content

Commit

Permalink
Change the TypeScript Level type to be a union instead of enum
Browse files Browse the repository at this point in the history
Fixes #373
Fixes #382
  • Loading branch information
sindresorhus committed Apr 2, 2020
1 parent 7f21f20 commit f0f4638
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 54 deletions.
47 changes: 23 additions & 24 deletions index.d.ts
@@ -1,25 +1,3 @@
declare const enum LevelEnum {
/**
All colors disabled.
*/
None = 0,

/**
Basic 16 colors support.
*/
Basic = 1,

/**
ANSI 256 colors support.
*/
Ansi256 = 2,

/**
Truecolor 16 million colors support.
*/
TrueColor = 3
}

/**
Basic foreground colors.
Expand Down Expand Up @@ -89,12 +67,26 @@ declare type Modifiers =
| 'visible';

declare namespace chalk {
type Level = LevelEnum;
/**
Levels:
- `0` - All colors disabled.
- `1` - Basic 16 colors support.
- `2` - ANSI 256 colors support.
- `3` - Truecolor 16 million colors support.
*/
type Level = 0 | 1 | 2 | 3;

interface Options {
/**
Specify the color support for Chalk.
By default, color support is automatically detected based on the environment.
Levels:
- `0` - All colors disabled.
- `1` - Basic 16 colors support.
- `2` - ANSI 256 colors support.
- `3` - Truecolor 16 million colors support.
*/
level?: Level;
}
Expand Down Expand Up @@ -161,7 +153,14 @@ declare namespace chalk {

/**
The color support for Chalk.
By default, color support is automatically detected based on the environment.
Levels:
- `0` - All colors disabled.
- `1` - Basic 16 colors support.
- `2` - ANSI 256 colors support.
- `3` - Truecolor 16 million colors support.
*/
level: Level;

Expand Down Expand Up @@ -400,7 +399,7 @@ This simply means that `chalk.red.yellow.green` is equivalent to `chalk.green`.
*/
declare const chalk: chalk.Chalk & chalk.ChalkFunction & {
supportsColor: chalk.ColorSupport | false;
Level: typeof LevelEnum;
Level: chalk.Level;
Color: Color;
ForegroundColor: ForegroundColor;
BackgroundColor: BackgroundColor;
Expand Down
6 changes: 0 additions & 6 deletions index.test-d.ts
Expand Up @@ -4,12 +4,6 @@ import chalk = require('.');
// - Helpers -
type colorReturn = chalk.Chalk & {supportsColor?: never};

// - Level -
expectType<number>(chalk.Level.None);
expectType<number>(chalk.Level.Basic);
expectType<number>(chalk.Level.Ansi256);
expectType<number>(chalk.Level.TrueColor);

// - supportsColor -
expectType<chalk.ColorSupport | false>(chalk.supportsColor);
expectType<boolean>((chalk.supportsColor as chalk.ColorSupport).hasBasic);
Expand Down
12 changes: 0 additions & 12 deletions source/index.js
Expand Up @@ -218,16 +218,4 @@ chalk.supportsColor = stdoutColor;
chalk.stderr = Chalk({level: stderrColor ? stderrColor.level : 0}); // eslint-disable-line new-cap
chalk.stderr.supportsColor = stderrColor;

// For TypeScript
chalk.Level = {
None: 0,
Basic: 1,
Ansi256: 2,
TrueColor: 3,
0: 'None',
1: 'Basic',
2: 'Ansi256',
3: 'TrueColor'
};

module.exports = chalk;
12 changes: 0 additions & 12 deletions test/level.js
Expand Up @@ -43,15 +43,3 @@ test('disable colors if they are not supported', async t => {
const {stdout} = await execa.node(path.join(__dirname, '_fixture'));
t.is(stdout, 'testout testerr');
});

test('chalk.Level enum object', t => {
const {Level} = chalk;
t.is(Level.None, 0);
t.is(Level.Basic, 1);
t.is(Level.Ansi256, 2);
t.is(Level.TrueColor, 3);
t.is(Level[Level.None], 'None');
t.is(Level[Level.Basic], 'Basic');
t.is(Level[Level.Ansi256], 'Ansi256');
t.is(Level[Level.TrueColor], 'TrueColor');
});

0 comments on commit f0f4638

Please sign in to comment.