ProgramGuru

TypeScript with npm Scripts

TypeScript with npm Scripts — why it matters

package.json scripts give a team repeatable commands for checking, building, testing, and starting TypeScript. Pin project dependencies and prefer invoking local tools with npm run to reduce version drift. A script is a shell command, not a TypeScript language feature.

What you’ll learn

  • Explain typescript with npm scripts and the problem it solves.
  • Identify the relevant compile-time checks and runtime responsibilities.
  • Run the example, predict its output, and try a realistic modification.

Understand the mechanism

The record models two familiar package scripts. The real package.json file is plain JSON read by npm; npm run check runs the configured command using local binaries.

npm run build only runs what the script says. It does not automatically test, lint, or deploy the package.

Follow the concept flow

TypeScript with npm Scripts — concept flowpackage scripts → npm run build/check → Local tool executionTypeScript with npm Scripts1package scripts2npm run build/check3Local tool executionCompiler configuration changes checks and output, not runtime guarantees.
Follow the responsibilities from input to checked result.

Run the primary TypeScript example

Save as npm-scripts.ts. The following program passed strict TypeScript compilation, and the displayed output was captured by executing its emitted JavaScript with Node.js 22.16.0.

TypeScript source

type Scripts = Record<string, string>;
const scripts: Scripts = { check: "tsc --noEmit", build: "tsc -p ." };
console.log(scripts.check);
console.log(scripts.build);

Verified output

tsc --noEmit
tsc -p .

Compile: npx tsc --strict --target es2022 --module commonjs npm-scripts.ts; run: node npm-scripts.js. For Node.js API examples, install @types/node first. For browser projects, use the browser-specific instructions below.

The example demonstrates the code-level behavior; the related configuration or test commands are shown separately when needed.

Trace the result

The record models two familiar package scripts. The real package.json file is plain JSON read by npm; npm run check runs the configured command using local binaries.

Predict what changes if you alter one input. Then run the code to compare your reasoning with the real output.

Try the full tool or configuration workflow

Practical project lab: These are real configuration and source-file examples, not a TypeScript object that merely describes settings. Place the files at the named paths relative to one project directory.

package.json

{"private":true,"scripts":{"check":"tsc --noEmit","build":"tsc -p ."},"devDependencies":{"typescript":"^5.8.3"}}

tsconfig.json

{"compilerOptions":{"target":"ES2022","module":"CommonJS","strict":true}}

main.ts

console.log("npm scripts");

Commands

npm install
npm run check
npm run build

The commands call the project-local TypeScript compiler.

This project requires its named external tool or dependency and was supplied as setup guidance; it was not claimed as an executed test here.

Common mistake and correction

npm run build only runs what the script says. It does not automatically test, lint, or deploy the package.

Identify whether the failure is a type-checking issue, a configuration issue, or a runtime behavior issue before choosing a fix.

Practice the concept

Write check and build scripts, install TypeScript locally, and run them both with npm.

Write down your expected result first. For configuration examples, compare compiler output and diagnostics; for test examples, make the assertion fail deliberately to verify that the tool reports it.

Check your understanding

What should you remember when configuring TypeScript compilation?
Choose one answer. You can try again.

Answer and explanation

Compiler options control static checks or emitted files; running the result is a separate step.

The record models two familiar package scripts. The real package.json file is plain JSON read by npm; npm run check runs the configured command using local binaries.

Official reference

For the exact current options and platform behavior, consult the official documentation. These examples were verified using the tool versions described above.

What to remember

package.

Run the primary example, then complete the practice task or environment-specific lab.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Using TypeScript with Bundlers →