Tersible
type Tersible<T = unknown> = T & { tersify(this: T, options?: Partial<TersifyOptions>): string}Tersible<T> is T plus a tersify() method. Any value of this shape controls its own output:
tersify() calls the method instead of applying the default rules,
unless raw: true is set.
The method receives the options the caller passed, so an implementation can honour maxLength.
Nothing enforces the cap on the value you return.
Producing one
Section titled “Producing one”tersible(subject, tersify?)returnsTersible<T>by defining the method on an existing value.Tersiblized(Base, tersify)returns a subclass whose instances have the method.- Declaring
tersify()on your own class satisfies the shape with no helper at all.
class Point { constructor( public x: number, public y: number ) {} tersify() { return `Point(${this.x}, ${this.y})` }}Using it in a signature
Section titled “Using it in a signature”import type { Tersible } from 'tersify'
function log(value: Tersible) { console.info(value.tersify({ maxLength: 80 }))}Tersible defaults its parameter to unknown, so the bare form accepts any self-formatting value.
See Custom output for the guide.