liveBook · Manning

Type compatibility

As explained in Functional Programming in JavaScript

The property that one function’s output type matches another function’s accepted input type.

Type compatibility means that one function returns a value another function can accept. It keeps function pipelines connected: for example, trim returns a String, and normalize accepts a String. JavaScript is loosely typed, so compatibility often depends on how a value behaves in practice rather than on declared type information. section 4.2

trim      :: String  -> String                     #1
normalize :: String  -> String                     #2

Read the function signatures as a compact compatibility check: String -> String shows that both functions accept and return strings. section 4.2

Cleaning a Social Security number

const trim = (str) => str.replace(/^\s*|\s*$/g, ''); const normalize = (str) => str.replace(/\-/g, ''); normalize(trim(' 444-44-4444 ')); // '444444444' trim produces a String, so normalize can receive its result. The same idea lets an isValid function receive the normalized String and return a Boolean. section 4.2

How Functional Programming in JavaScript covers it

How deep it goes
Defines output-input matching, explains duck typing in JavaScript, and stops short of covering static type systems.
Examples
Uses trim, normalize, and isValid to process a Social Security number through compatible function stages.
What you can do after
Apply it via function signatures to design pipelines whose stages accept one another’s outputs.
How it connects
Ties type compatibility to function pipelines, arity, duck typing, and type systems.

Also explained in

Compare how 3 books explain type compatibility →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Functional Programming in JavaScript. The section links point to the parts of the book it draws on.