Skip to content

Commit

Permalink
fix(TS): TS2339: Property strip does not exist on type when the TS co…
Browse files Browse the repository at this point in the history
…mpiler option `module` is `node16`
  • Loading branch information
webdiscus committed Dec 27, 2024
1 parent 48a7450 commit 553fd5f
Show file tree
Hide file tree
Showing 18 changed files with 382 additions and 130 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Change log

## 3.5.2 (2024-12-28)

- fix: TS2339: Property 'strip' does not exist on type when the TS compiler option `module` is `node16`
- refactor: optimize index.d.ts to reduce package size from 7.3 kB to 7.0 kB

## 3.5.1 (2024-12-26)

- refactor: invisible code optimisation
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ansis",
"version": "3.5.1",
"version": "3.5.2",
"description": "A small and fast Node.js library for applying ANSI colors and styles in terminal output",
"keywords": [
"ansi",
Expand Down
2 changes: 1 addition & 1 deletion package.npm.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ansis",
"version": "3.5.1",
"version": "3.5.2",
"description": "ANSI colors and styles in terminal output",
"keywords": [
"ansi",
Expand Down
214 changes: 89 additions & 125 deletions src/index.type.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type AnsiStyles =
| 'strike' /** S̶t̶r̶i̶k̶e̶t̶h̶r̶o̶u̶g̶h̶ style. (Not widely supported) Alias for `strikethrough`. */;

// BasicColors
type BC = 'black' | 'red' | 'green' | 'yellow' | 'blue' | 'magenta' | 'cyan' | 'white' | 'gray' | 'grey';
type BC = 'black' | 'red' | 'green' | 'yellow' | 'blue' | 'magenta' | 'cyan' | 'white';

// BrightColors
type BBC = `${BC}Bright`;
Expand All @@ -39,9 +39,11 @@ type BBC = `${BC}Bright`;
*/
type AnsiColors =
| BC
| 'gray' | 'grey'
| BBC
| `bg${Capitalize<BC>}`
| `bg${Capitalize<BBC>}`;
| `bg${Capitalize<BBC>}`
| 'bgGray' | 'bgGrey';

// Short alias for Ansis
type AC = AnsiColors;
Expand All @@ -55,19 +57,7 @@ type DP = {

// Static properties and methods (StaticMethods)
type SP = {
/**
* Whether the output supports ANSI color and styles.
*
* @return {boolean}
*/
isSupported(): boolean;

/**
* Return styled string.
*
* @param {string | TemplateStringsArray} string
*/
(string: string): string;
(value: unknown): string;

(strings: TemplateStringsArray, ...values: any[]): string;

Expand Down Expand Up @@ -189,6 +179,13 @@ type SP = {
*/
extend<U extends string>(colors: Record<U, string | { open: string; close: string }>): asserts this is InstanceType<typeof Ansis> & Record<U, A>;

/**
* Whether the output supports ANSI color and styles.
*
* @return {boolean}
*/
isSupported(): boolean;

/** The ANSI escape sequences for starting the current style. */
open: string;

Expand All @@ -201,128 +198,95 @@ type Ansis = SP & DP;
// Short alias for Ansis
type A = Ansis;

// Note: define constants with only unique declarations,
// E.g. the methods rgb and bgRgb have the same arguments and return, therefore we need it only once.
declare const Ansis: new () => A,
ansi256: (code: number) => A,
bgAnsi256: (code: number) => A,
fg: (code: number) => A,
bg: (code: number) => A,
rgb: (r: number, g: number, b: number) => A,
bgRgb: (r: number, g: number, b: number) => A,
hex: (code: string) => A,
bgHex: (code: string) => A,

ansis: A,

// Base styles
reset: A,
inverse: A,
hidden: A,
visible: A,

// Text styles
bold: A,
dim: A,
italic: A,
underline: A,
strikethrough: A,
strike: A,
isSupported: () => boolean,
strip: SP["strip"],
extend: SP["extend"],

// Foreground colors
black: A,
red: A,
green: A,
yellow: A,
blue: A,
magenta: A,
cyan: A,
white: A,
gray: A,
grey: A,
blackBright: A,
redBright: A,
greenBright: A,
yellowBright: A,
blueBright: A,
magentaBright: A,
cyanBright: A,
whiteBright: A,
fg: SP["fg"],
rgb: SP["rgb"],
hex: SP["hex"],

// Background colors
bgBlack: A,
bgRed: A,
bgGreen: A,
bgYellow: A,
bgBlue: A,
bgMagenta: A,
bgCyan: A,
bgWhite: A,
bgBlackBright: A,
bgRedBright: A,
bgGreenBright: A,
bgYellowBright: A,
bgBlueBright: A,
bgMagentaBright: A,
bgCyanBright: A,
bgWhiteBright: A;
// declare all styles and colors of type Ansis
a: A;

// Named exports
export {
// note for me in feature: AnsiColors, AnsiStyles and AnsiColorsExtend types used in many project on GitHub, don't remove it!
type AnsiColors,
type AnsiStyles,
type AnsiColorsExtend,
ansis as default,
a as default,
Ansis,
ansi256,

// exporting these methods is required if the `module` compiler option is `node16,
// otherwise the TS compiler can't find they in default import:
// import ansis from 'ansis';
// ansis.strip(text); // <= TS2339: Property strip does not exist on type
isSupported,
strip,
extend,

fg,
bgAnsi256,
bg,
fg as bg,
fg as ansi256,
fg as bgAnsi256,
rgb,
bgRgb,
rgb as bgRgb,
hex,
bgHex,
reset,
inverse,
hidden,
visible,
bold,
dim,
italic,
underline,
strikethrough,
strike,
black,
red,
green,
yellow,
blue,
magenta,
cyan,
white,
gray,
grey,
blackBright,
redBright,
greenBright,
yellowBright,
blueBright,
magentaBright,
cyanBright,
whiteBright,
bgBlack,
bgRed,
bgGreen,
bgYellow,
bgBlue,
bgMagenta,
bgCyan,
bgWhite,
bgBlackBright,
bgRedBright,
bgGreenBright,
bgYellowBright,
bgBlueBright,
bgMagentaBright,
bgCyanBright,
bgWhiteBright,
hex as bgHex,

// Base styles
a as reset,
a as inverse,
a as hidden,
a as visible,
a as bold,
a as dim,
a as italic,
a as underline,
a as strikethrough,
a as strike,

// Foreground colors
a as black,
a as red,
a as green,
a as yellow,
a as blue,
a as magenta,
a as cyan,
a as white,
a as gray,
a as grey,
a as blackBright,
a as redBright,
a as greenBright,
a as yellowBright,
a as blueBright,
a as magentaBright,
a as cyanBright,
a as whiteBright,

// Background colors
a as bgBlack,
a as bgGray,
a as bgGrey,
a as bgRed,
a as bgGreen,
a as bgYellow,
a as bgBlue,
a as bgMagenta,
a as bgCyan,
a as bgWhite,
a as bgBlackBright,
a as bgRedBright,
a as bgGreenBright,
a as bgYellowBright,
a as bgBlueBright,
a as bgMagentaBright,
a as bgCyanBright,
a as bgWhiteBright,
};
1 change: 0 additions & 1 deletion test/manual-autocomplete-d.ts/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// NEW named import, >= v1.6.0
import ansis, { Ansis, AnsiColorsExtend, red, green, blue, yellow, magenta } from 'ansis';

const log = console.log;
Expand Down
6 changes: 6 additions & 0 deletions test/ts-compiler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,10 @@ describe('imports', () => {
test('extend colors; tsc', () => executeTSFile('ts/extend-colors', 'tsc'));
test('extend colors; swc', () => executeTSFile('ts/extend-colors', 'swc'));
test('extend colors; esbuild', () => executeTSFile('ts/extend-colors', 'esbuild'));

});

describe('imports from package with type module', () => {
test('module ESNext', () => executeTSFile('ts/module-import-esnext', 'tsc'));
test('module Node16', () => executeTSFile('ts/module-import-node16', 'tsc'));
});
15 changes: 15 additions & 0 deletions test/ts/module-import-esnext/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"jsc": {
"target": "ES6",
"parser": {
"syntax": "typescript", // or "ecmascript" if you're using plain JavaScript
"tsx": false, // set to true if using React/TSX
"decorators": true // set to true if using decorators
}
},
"module": {
"type": "commonjs", // Default to ES modules
"strict": true, // Enable strict ES module output
"noInterop": true // Disable interop for default and named exports
}
}
20 changes: 20 additions & 0 deletions test/ts/module-import-esnext/expected/index.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
isSupported: true
bgAnsi256: 1993
bgAnsi256: true
Hello, World!
red
Hello World!
default: red
default: cyanBright
extended: pink
extended: orange
extended color: apple italic
destructured default color: red
destructured extended color: pink
destructured extended color: orange italic
extended chained color: pink underline
destructured extended chained color: pink underline
gray
 bgGray 
 bgCyanBright 
 bgWhiteBright 
10 changes: 10 additions & 0 deletions test/ts/module-import-esnext/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"type": "module",
"scripts": {
"check": "rimraf --glob dist/*.* && tsc",
"build": "rimraf --glob dist/*.* && tsc && node dist/index.js --color=true > dist/index.out",
"build:swc": "rimraf --glob dist/*.* && swc ./src -d dist && node dist/index.js --color=true > dist/index.out",
"build:esbuild": "rimraf --glob dist/*.* && esbuild src/index.ts --bundle --outfile=dist/index.js && node dist/index.js --color=true > dist/index.out",
"view": "node ./dist/index.js"
}
}
Loading

0 comments on commit 553fd5f

Please sign in to comment.