Publishing packages
What to publish
Publish compiled JavaScript plus declaration files, never raw sources alone. Consumers run the output directly and type check against the bundled types.
Keep the package lean: compiled files, types, README, and license. Tests and drafts stay in the repository where they belong.
tsc --declaration
A clean build prints nothing and writes the compiled files with their declaration files beside them.
Types field
The types field points the checker at the entry declaration file. Editors and builds follow it automatically, so users get completions with zero setup.
Pair it with the main entry for JavaScript. The two fields together describe one package in two languages: runtime and types.
{ "name": "greetlib", "version": "1.0.0", "main": "index.js", "types": "index.d.ts" }
Semver sketch
Versions signal risk with three numbers: major for breaking changes, minor for new features, and patch for safe fixes. Users read the bump and decide how carefully to upgrade.
Keep the public API small so most releases stay minor or patch. Every exported function is a promise you must keep in the next major line.
export function greet(name: string): string { return "Hello, " + name; } console.log(greet("Ada"));
Hello, Ada
README first
Write the README before release and start with a runnable example. Installation fades from memory, but a copy paste snippet proves the package works in seconds.
Document one happy path well instead of every option briefly. Users adopt packages with clear first five minutes, not with long option lists.
import { greet } from "greetlib"; console.log(greet("Ada"));
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What should a TypeScript package publish?
Answer
Compiled JavaScript plus declaration files
Consumers run the output and type check against the declarations, so both ship.
-
What does the
typesfield of package.json point at?Answer
The entry declaration file of the package
Editors and builds follow the field to find the declarations without any setup.
-
What does the first number of a semver version signal?
Answer
A breaking change in the public API
Major bumps mark breaks, minor bumps add features, and patch bumps fix bugs.
-
What should a README start with?
Answer
A runnable example of the happy path
A small paste-ready example proves the package works in the first five minutes.
-
What does this program print?
export function greet(name: string): string { return "Hello, " + name; } console.log(greet("Ada"));Answer
Hello, Ada
The export marks the name for other files, and the local call still prints the greeting.
-
What does this program print?
console.log("1.0.0".split(".").length);Answer
3
The string splits at the two dots, which makes three parts.
-
What does this program print?
console.log(["major", "minor", "patch"][1]);Answer
minor
The index 1 names the second element of the array.
-
What does this program print?
console.log(1 + 2 + "3");Answer
33
The numbers add to 3 first, and then the string joins to make 33.
-
What does the compiler say about this program?
export function greet(name: string): string { return "Hello, " + name; } console.log(greet(42));Answer
error TS2345: Argument of type 'number' is not assignable to parameter of type 'string'.
The parameter is a string, and the number 42 does not fit it.
-
What does the compiler say about this program?
const version: string = 1.0; console.log(version);Answer
error TS2322: Type 'number' is not assignable to type 'string'.
A number literal cannot be stored in a variable typed as string.
Article author: Arthur Isaev