Skip to content

TypeScript

csv-pipe is written in TypeScript and ships its own types. There is no any in the public surface, and your record type drives the API.

Hover any token in the examples below to see the inferred types.

Typing your records

Pass any object type. stringify and createCsvEncoder infer the element type, so columns and the format hook are typed against it.

ts
import { 
stringify
} from 'csv-pipe';
type
User
= {
name
: string;
email
: string;
age
: number };
const
users
:
User
[] = [
{
name
: 'Alex Johnson',
email
: 'alex@example.com',
age
: 29 }
]; const
csv
=
stringify
(
users
);

Columns are checked against your type

columns accepts the keys of your record, or a map of key to header label. A key that does not exist is a compile error.

ts
stringify
(
users
, {
columns
: ['name', 'emial'] });
Type '"emial"' is not assignable to type '"name" | "email" | "age"'. Did you mean '"email"'?

So a renamed or removed field surfaces at compile time, across every call site, instead of producing a file with an empty column.

A reusable, typed encoder

createCsvEncoder<T> returns an encoder bound to your type, with typed row and stream.

ts
const 
toCsv
=
createCsvEncoder
<
User
>({
columns
: ['name', 'email'] });
toCsv
(
users
);
toCsv
.
row
(
users
[0]);

Typed parsing

Parsing is typed too. parse<T> and createCsvParser<T> infer the record type, and columns is checked against it, just as with encoding. See Typing and validation for dynamic typing and runtime validation.

Exported types

ts
import type {
  CsvOptions,
  CsvColumns,
  CsvEncoder,
  CsvRecord,
  CsvInput,
  CsvCell,
  CsvPrimitive,
  QuotingMode,
  BooleanStyle,
  CsvFormatter,
  CsvFormatContext
} from 'csv-pipe';

The CsvPipeError class is also exported as a value. See the API reference for each definition.

Released under the MIT License.