append-type

Stringify the value with appending its type: 10 → '10 (number)'

Usage no npm install needed!

<script type="module">
  import appendType from 'https://cdn.skypack.dev/append-type';
</script>

README

append-type

npm version Build Status Coverage Status

Stringify the value with appending its type: 10'10 (number)'

import appendType from 'append-type';

appendType('123'); //=> '123 (string)'
appendType(123); //=> '123 (number)'

Installation

Use npm.

npm install append-type

API

import appendType from 'append-type';

appendType(value)

value: any type
Return: string

Essentially, it returns String(value) + ' (' + typeof value + ')'.

appendType(() => {}); //=> '() => {} (function)'

When it takes null / undefined, it returns 'null' / 'undefined'.

appendType(null); //=> 'null'
appendType(undefined); //=> 'undefined'

Example

This module is useful for making TypeError error messages.

function reverse(v) {
  if (typeof v !== 'boolean') {
    throw new TypeError(`Expected a Boolean value, but got ${appendType(v)}.`);
  }

  return !v;
};

reverse(1); //=> TypeError: Expected a Boolean value, but got 1 (number).

License

MIT No Attribution © 2019 Shinnosuke Watanabe