question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Chalk.js how to fill in TODOs

See original GitHub issue

I tried running ts2fable against chalk using the declarations included with the library. (not from definitelytyped). I got the following error message about 40 or so times while running ts2fable.

unsupported printType This
unsupported printType This
unsupported printType This
unsupported printType This
unsupported printType This
unsupported printType This
unsupported printType This
unsupported printType This
unsupported printType This
unsupported printType This

The output looks like this:

module rec chalk
open System
open Fable.Core
open Fable.Import.JS

type [<AllowNullLiteral>] IExports =
    abstract ChalkConstructor: ChalkConstructorStatic with get, set

type [<RequireQualifiedAccess>] Level =
    | None = 0
    | Basic = 1
    | Ansi256 = 2
    | TrueColor = 3

type [<AllowNullLiteral>] ChalkOptions =
    abstract enabled: bool option with get, set
    abstract level: Level option with get, set

type [<AllowNullLiteral>] ChalkConstructor =
    [<Emit "$0($1...)">] abstract Invoke: ?options: ChalkOptions -> Chalk

type [<AllowNullLiteral>] ChalkConstructorStatic =
    [<Emit "new $0($1...)">] abstract Create: ?options: ChalkOptions -> ChalkConstructor

type [<AllowNullLiteral>] ColorSupport =
    abstract level: Level with get, set
    abstract hasBasic: bool with get, set
    abstract has256: bool with get, set
    abstract has16m: bool with get, set

type [<AllowNullLiteral>] Chalk =
    [<Emit "$0($1...)">] abstract Invoke: [<ParamArray>] text: string -> string
    [<Emit "$0($1...)">] abstract Invoke: text: TemplateStringsArray * [<ParamArray>] placeholders: string -> string
    abstract ``constructor``: ChalkConstructor with get, set
    abstract enabled: bool with get, set
    abstract level: Level with get, set
    abstract rgb: r: float * g: float * b: float -> Chalk
    abstract hsl: h: float * s: float * l: float -> Chalk
    abstract hsv: h: float * s: float * v: float -> Chalk
    abstract hwb: h: float * w: float * b: float -> Chalk
    abstract bgHex: color: string -> Chalk
    abstract bgKeyword: color: string -> Chalk
    abstract bgRgb: r: float * g: float * b: float -> Chalk
    abstract bgHsl: h: float * s: float * l: float -> Chalk
    abstract bgHsv: h: float * s: float * v: float -> Chalk
    abstract bgHwb: h: float * w: float * b: float -> Chalk
    abstract hex: color: string -> Chalk
    abstract keyword: color: string -> Chalk
    abstract reset: TODO with get, set
    abstract bold: TODO with get, set
    abstract dim: TODO with get, set
    abstract italic: TODO with get, set
    abstract underline: TODO with get, set
    abstract inverse: TODO with get, set
    abstract hidden: TODO with get, set
    abstract strikethrough: TODO with get, set
    abstract visible: TODO with get, set
    abstract black: TODO with get, set
    abstract red: TODO with get, set
    abstract green: TODO with get, set
    abstract yellow: TODO with get, set
    abstract blue: TODO with get, set
    abstract magenta: TODO with get, set
    abstract cyan: TODO with get, set
    abstract white: TODO with get, set
    abstract gray: TODO with get, set
    abstract grey: TODO with get, set
    abstract blackBright: TODO with get, set
    abstract redBright: TODO with get, set
    abstract greenBright: TODO with get, set
    abstract yellowBright: TODO with get, set
    abstract blueBright: TODO with get, set
    abstract magentaBright: TODO with get, set
    abstract cyanBright: TODO with get, set
    abstract whiteBright: TODO with get, set
    abstract bgBlack: TODO with get, set
    abstract bgRed: TODO with get, set
    abstract bgGreen: TODO with get, set
    abstract bgYellow: TODO with get, set
    abstract bgBlue: TODO with get, set
    abstract bgMagenta: TODO with get, set
    abstract bgCyan: TODO with get, set
    abstract bgWhite: TODO with get, set
    abstract bgBlackBright: TODO with get, set
    abstract bgRedBright: TODO with get, set
    abstract bgGreenBright: TODO with get, set
    abstract bgYellowBright: TODO with get, set
    abstract bgBlueBright: TODO with get, set
    abstract bgMagentaBright: TODO with get, set
    abstract bgCyanBright: TODO with get, set
    abstract bgWhiteBright: TODO with get, set

let [<Global>] chalk: obj = jsNative

Notice all the “TODO” entries. I am not sure what to replace these with. This is a fluent API to be used like this from JavaScript:

// Combine styled and normal strings
log(chalk.blue('Hello') + 'World' + chalk.red('!'));

// Compose multiple styles using the chainable API
log(chalk.blue.bgRed.bold('Hello world!'));

// Pass in multiple arguments
log(chalk.blue('Hello', 'World!', 'Foo', 'bar', 'biz', 'baz'));

What do I need to do to get this working and how would I consume from fable/f#?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
ctaggartcommented, Nov 13, 2017

To try it out: yarn global add ts2fable@next. It is quite a fun library.

1reaction
mike-morrcommented, Nov 12, 2017

Ok, this is working:

module rec Chalk
open System
open Fable.Core
open Fable.Import.JS

type [<AllowNullLiteral>] IExports =
    abstract ChalkConstructor: ChalkConstructorStatic with get, set

type [<RequireQualifiedAccess>] Level =
    | None = 0
    | Basic = 1
    | Ansi256 = 2
    | TrueColor = 3

type [<AllowNullLiteral>] ChalkOptions =
    abstract enabled: bool option with get, set
    abstract level: Level option with get, set

type [<AllowNullLiteral>] ChalkConstructor =
    [<Emit "$0($1...)">] abstract Invoke: ?options: ChalkOptions -> Chalk

type [<AllowNullLiteral>] ChalkConstructorStatic =
    [<Emit "new $0($1...)">] abstract Create: ?options: ChalkOptions -> ChalkConstructor

type [<AllowNullLiteral>] ColorSupport =
    abstract level: Level with get, set
    abstract hasBasic: bool with get, set
    abstract has256: bool with get, set
    abstract has16m: bool with get, set

type [<AllowNullLiteral>] Chalk =
    [<Emit "$0($1...)">] abstract Invoke: [<ParamArray>] text: string -> string
    [<Emit "$0($1...)">] abstract Invoke: text: TemplateStringsArray * [<ParamArray>] placeholders: string -> string
    abstract ``constructor``: ChalkConstructor with get, set
    abstract enabled: bool with get, set
    abstract level: Level with get, set
    abstract rgb: r: float * g: float * b: float -> Chalk
    abstract hsl: h: float * s: float * l: float -> Chalk
    abstract hsv: h: float * s: float * v: float -> Chalk
    abstract hwb: h: float * w: float * b: float -> Chalk
    abstract bgHex: color: string -> Chalk
    abstract bgKeyword: color: string -> Chalk
    abstract bgRgb: r: float * g: float * b: float -> Chalk
    abstract bgHsl: h: float * s: float * l: float -> Chalk
    abstract bgHsv: h: float * s: float * v: float -> Chalk
    abstract bgHwb: h: float * w: float * b: float -> Chalk
    abstract hex: color: string -> Chalk
    abstract keyword: color: string -> Chalk
    abstract reset: value: string -> Chalk
    abstract bold: value: string -> string
    abstract dim: value: string -> string
    abstract italic: value: string -> string
    abstract underline: value: string -> string
    abstract inverse: value: string -> string
    abstract hidden: value: string -> string
    abstract strikethrough: value: string -> string
    abstract visible: value: string -> string
    abstract black: value: string -> string
    abstract red: value: string -> string
    abstract green: value: string -> string
    abstract yellow: value: string -> string
    abstract blue: value: string -> string
    abstract magenta: value: string -> string
    abstract cyan: value: string -> string
    abstract white: value: string -> string
    abstract gray: value: string -> string
    abstract grey: value: string -> string
    abstract blackBright: value: string -> string
    abstract redBright: value: string -> string
    abstract greenBright: value: string -> string
    abstract yellowBright: value: string -> string
    abstract blueBright: value: string -> string
    abstract magentaBright: value: string -> string
    abstract cyanBright: value: string -> string
    abstract whiteBright: value: string -> string
    abstract bgBlack: value: string -> string
    abstract bgRed: value: string -> string
    abstract bgGreen: value: string -> string
    abstract bgYellow: value: string -> string
    abstract bgBlue: value: string -> string
    abstract bgMagenta: value: string -> string
    abstract bgCyan: value: string -> string
    abstract bgWhite: value: string -> string
    abstract bgBlackBright: value: string -> string
    abstract bgRedBright: value: string -> string
    abstract bgGreenBright: value: string -> string
    abstract bgYellowBright: value: string -> string
    abstract bgBlueBright: value: string -> string
    abstract bgMagentaBright: value: string -> string
    abstract bgCyanBright: value: string -> string
    abstract bgWhiteBright: value: string -> string

let [<Global>] chalk: Chalk = jsNative

Note the biggest change is to function of string -> string for most of the methods and allows syntax like.

open Fable.Core
open Fable.Core.JsInterop
open Fable.Import
open Chalk
let chalk: Chalk = importDefault "chalk"

let init() = 
  let stringToPrint =
    "This is a test string"
    |> chalk.blue
    |> chalk.bold
    |> chalk.underline
    |> chalk.bgWhite

  printfn "%s" stringToPrint

init()

I was surprised that I had to add the importDefault to the code, I expected open Chalk to import the dependency so I could just use it. Could be my misunderstanding though.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Dumping objects outputs [object Object] #118 - chalk/chalk
Chalk outputting contents of objects is opinionated - meaning it breaks the basic JavaScript contract of .toString() showing us the string equivalent of...
Read more >
How to use the chalk.greenBright function in chalk
To help you get started, we've selected a few chalk. ... root => f => { // TODO: Add logic for modifications, new,...
Read more >
Colorize your console output with Chalk! - Node.js ... - YouTube
Chalk NPM package link: https://www.npmjs.com/package/ chalk In this video I'll be demonstrating how you can use the hugely popular NPM ...
Read more >
How to Highlight A Terminal Output with Node.JS using Chalk
In this video, we are going to explore another popular NPM package called Chalk. Simply put, Chalk allows us to do Terminal styling...
Read more >
Getting started with oclif by creating a todo cli app
The oclif generator creates a CLI project in either JavaScript or TypeScript to get you started quickly. It requires very few runtime ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found