diff --git a/index.js b/index.js index 4c81d6d..de65bd4 100644 --- a/index.js +++ b/index.js @@ -218,3 +218,4 @@ Object.defineProperties(Chalk.prototype, styles); module.exports = Chalk(); // eslint-disable-line new-cap module.exports.supportsColor = supportsColor; +module.exports.default = module.exports; // For TypeScript diff --git a/package.json b/package.json index a3152ee..084aea6 100644 --- a/package.json +++ b/package.json @@ -8,13 +8,14 @@ "node": ">=4" }, "scripts": { - "test": "xo && nyc ava", + "test": "xo && tsc --project types && nyc ava", "bench": "matcha benchmark.js", "coveralls": "nyc report --reporter=text-lcov | coveralls" }, "files": [ "index.js", - "templates.js" + "templates.js", + "types/index.d.ts" ], "keywords": [ "color", @@ -52,8 +53,10 @@ "matcha": "^0.7.0", "nyc": "^11.0.2", "resolve-from": "^3.0.0", + "typescript": "^2.5.3", "xo": "*" }, + "types": "types/index.d.ts", "xo": { "envs": [ "node", diff --git a/types/index.d.ts b/types/index.d.ts new file mode 100644 index 0000000..7505746 --- /dev/null +++ b/types/index.d.ts @@ -0,0 +1,90 @@ +// Type definitions for Chalk +// Definitions by: Thomas Sauer + +export const enum Level { + None = 0, + Basic = 1, + Ansi256 = 2, + TrueColor = 3 +} + +export interface ChalkOptions { + enabled?: boolean; + level?: Level; +} + +export interface Chalk { + new (options?: ChalkOptions): Chalk; + (options?: ChalkOptions): Chalk; + (...text: string[]): string; + (text: TemplateStringsArray, ...placeholders: string[]): string; + constructor: Chalk; + enabled: boolean; + level: Level; + supportsColor: { + level: Level; + hasBasic: boolean; + has256: boolean; + has16m: boolean; + }; + rgb(r: number, g: number, b: number): Chalk; + hsl(h: number, s: number, l: number): Chalk; + hsv(h: number, s: number, v: number): Chalk; + hwb(h: number, w: number, b: number): Chalk; + bgHex(color: string): Chalk; + bgKeyword(color: string): Chalk; + bgRgb(r: number, g: number, b: number): Chalk; + bgHsl(h: number, s: number, l: number): Chalk; + bgHsv(h: number, s: number, v: number): Chalk; + bgHwb(h: number, w: number, b: number): Chalk; + hex(color: string): Chalk; + keyword(color: string): Chalk; + + reset: Chalk; + bold: Chalk; + dim: Chalk; + italic: Chalk; + underline: Chalk; + inverse: Chalk; + hidden: Chalk; + strikethrough: Chalk; + + black: Chalk; + red: Chalk; + green: Chalk; + yellow: Chalk; + blue: Chalk; + magenta: Chalk; + cyan: Chalk; + white: Chalk; + gray: Chalk; + grey: Chalk; + blackBright: Chalk; + redBright: Chalk; + greenBright: Chalk; + yellowBright: Chalk; + blueBright: Chalk; + magentaBright: Chalk; + cyanBright: Chalk; + whiteBright: Chalk; + + bgBlack: Chalk; + bgRed: Chalk; + bgGreen: Chalk; + bgYellow: Chalk; + bgBlue: Chalk; + bgMagenta: Chalk; + bgCyan: Chalk; + bgWhite: Chalk; + bgBlackBright: Chalk; + bgRedBright: Chalk; + bgGreenBright: Chalk; + bgYellowBright: Chalk; + bgBlueBright: Chalk; + bgMagentaBright: Chalk; + bgCyanBright: Chalk; + bgWhiteBright: Chalk; +} + +declare function chalk (): any; +export default chalk as Chalk; diff --git a/types/test.ts b/types/test.ts new file mode 100644 index 0000000..01509fd --- /dev/null +++ b/types/test.ts @@ -0,0 +1,47 @@ +import chalk, {Level} from '..'; + +chalk.underline('foo'); +chalk.red('foo'); +chalk.bgRed('foo'); + +const name = 'Josh'; +chalk`Hello {bold.red ${name}}`; + +chalk.red`foo`; +chalk.underline`foo`; +chalk`foo`; + +chalk.red.bgGreen.underline('foo'); +chalk.underline.red.bgGreen('foo'); + +chalk.grey('foo'); + +chalk.constructor({level: 1}); +const ctx = chalk.constructor({level: Level.TrueColor }); +ctx('foo'); +ctx.red('foo'); +ctx`foo`; + +chalk.enabled = true; +chalk.level = 1; +chalk.level = Level.Ansi256; + +chalk.level === Level.Ansi256; + +let chalkInstance = new chalk(); +chalkInstance = new chalk.constructor(); +chalkInstance = chalk.constructor(); + +chalk.enabled; +chalk.level; +chalk.supportsColor.level; +chalk.supportsColor.has16m; +chalk.supportsColor.has256; +chalk.supportsColor.hasBasic; + +chalk.keyword('orange').bgBlue('foo'); +chalk.hex('#123456').bgBlue('foo'); +chalk.rgb(1, 14, 9).bgBlue('foo'); +chalk.hsl(1, 14, 9).bgBlue('foo'); +chalk.hsv(1, 14, 9).bgBlue('foo'); +chalk.hwb(1, 14, 9).bgBlue('foo'); diff --git a/types/tsconfig.json b/types/tsconfig.json new file mode 100644 index 0000000..b73840f --- /dev/null +++ b/types/tsconfig.json @@ -0,0 +1,9 @@ +{ + "compilerOptions": { + "module": "commonjs", + "target": "es6", + "noImplicitAny": true, + "noEmit": true, + "allowJs": true + } +}