diff --git a/package.json b/package.json index 2501c72..cd2e7de 100644 --- a/package.json +++ b/package.json @@ -8,13 +8,14 @@ "node": ">=4" }, "scripts": { - "test": "xo && nyc mocha", + "test": "xo && tsc --project types && nyc mocha", "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 @@ "mocha": "*", "nyc": "^11.0.2", "resolve-from": "^3.0.0", + "typescript": "^2.4.1", "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..1b7079e --- /dev/null +++ b/types/index.d.ts @@ -0,0 +1,87 @@ +// Type definitions for Chalk +// Definitions by: Thomas Sauer + +export default chalk; + +declare function chalk(...text: string[]): string; +declare function chalk(text: TemplateStringsArray, ...placeholders: string[]): string; + +declare namespace chalk { + + export enum Level { + None = 0, + Basic = 1, + Extended = 2, + TrueColor = 3 + } + + interface ConstructorOptions { + enabled?: boolean; + level?: Level; + } + + export function constructor(options?: ConstructorOptions): typeof chalk; + + export let enabled: boolean; + export let level: Level; + export const supportsColor: boolean; + + export const hex: (color: string) => typeof chalk; + export const keyword: (color: string) => typeof chalk; + export const rgb: (r: number, g: number, b: number) => typeof chalk; + export const hsl: (h: number, s: number, l: number) => typeof chalk; + export const hsv: (h: number, s: number, v: number) => typeof chalk; + export const hwb: (h: number, w: number, b: number) => typeof chalk; + + export const bgHex: (color: string) => typeof chalk; + export const bgKeyword: (color: string) => typeof chalk; + export const bgRgb: (r: number, g: number, b: number) => typeof chalk; + export const bgHsl: (h: number, s: number, l: number) => typeof chalk; + export const bgHsv: (h: number, s: number, v: number) => typeof chalk; + export const bgHwb: (h: number, w: number, b: number) => typeof chalk; + + export const reset: typeof chalk; + export const bold: typeof chalk; + export const dim: typeof chalk; + export const italic: typeof chalk; + export const underline: typeof chalk; + export const inverse: typeof chalk; + export const hidden: typeof chalk; + export const strikethrough: typeof chalk; + + export const black: typeof chalk; + export const red: typeof chalk; + export const green: typeof chalk; + export const yellow: typeof chalk; + export const blue: typeof chalk; + export const magenta: typeof chalk; + export const cyan: typeof chalk; + export const white: typeof chalk; + export const gray: typeof chalk; + export const grey: typeof gray; + export const blackBright: typeof chalk; + export const redBright: typeof chalk; + export const greenBright: typeof chalk; + export const yellowBright: typeof chalk; + export const blueBright: typeof chalk; + export const magentaBright: typeof chalk; + export const cyanBright: typeof chalk; + export const whiteBright: typeof chalk; + + export const bgBlack: typeof chalk; + export const bgRed: typeof chalk; + export const bgGreen: typeof chalk; + export const bgYellow: typeof chalk; + export const bgBlue: typeof chalk; + export const bgMagenta: typeof chalk; + export const bgCyan: typeof chalk; + export const bgWhite: typeof chalk; + export const bgBlackBright: typeof chalk; + export const bgRedBright: typeof chalk; + export const bgGreenBright: typeof chalk; + export const bgYellowBright: typeof chalk; + export const bgBlueBright: typeof chalk; + export const bgMagentaBright: typeof chalk; + export const bgCyanBright: typeof chalk; + export const bgWhiteBright: typeof chalk; +} diff --git a/types/test.ts b/types/test.ts new file mode 100644 index 0000000..d0e6aa4 --- /dev/null +++ b/types/test.ts @@ -0,0 +1,40 @@ +import chalk 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: chalk.Level.TrueColor }); +ctx('foo'); +ctx.red('foo'); +ctx`foo`; + +chalk.enabled = true; +chalk.level = 1; +chalk.level = chalk.Level.Extended; + +chalk.level === chalk.Level.Extended; + +chalk.enabled; +chalk.supportsColor; +chalk.level; + +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..af436cc --- /dev/null +++ b/types/tsconfig.json @@ -0,0 +1,9 @@ +{ + "compilerOptions": { + "module": "commonjs", + "target": "es6", + "noImplicitAny": true, + "noEmit": true, + "allowJs": true + } +}