home / skills / nevaberry / nevaberry-plugins / typescript-knowledge-patch

This skill updates your TypeScript workflow with 5.9+ features like import defer, node20 module behavior, and ArrayBuffer handling guidance.

npx playbooks add skill nevaberry/nevaberry-plugins --skill typescript-knowledge-patch

Review the files below or copy the command above to add this skill to your agents.

Files (2)
SKILL.md
3.0 KB
---
name: typescript-knowledge-patch
description: This skill should be used when writing TypeScript code, using "import defer", "--module node20", "tsc --init", working with ArrayBuffer types, or any TypeScript features from version 5.9 onwards.
license: MIT
metadata:
  author: Nevaberry
  version: "5.9"
---

# TypeScript 5.9+ Knowledge Patch

Claude's baseline knowledge covers TypeScript through 5.8. This skill provides features from 5.9 (May 2025) onwards.

## 5.9 (2025-05-22)

### `import defer` Syntax

New ECMAScript proposal for deferred module evaluation. The module's code isn't executed until you access one of its exports, enabling lazy loading without dynamic `import()`.

```typescript
import defer * as feature from "./some-feature.js";

// Module not executed yet
console.log("Starting up...");

// Module executes NOW when property is accessed
console.log(feature.specialConstant);
```

**Constraints:**
- Only namespace imports allowed (`import defer * as name`)
- No named imports (`import defer { x }`) or default imports
- Requires `--module preserve` or `--module esnext`

### `--module node20`

Stable module option modeling Node.js 20 behavior. Unlike `nodenext` (which tracks latest Node.js):
- Remains stable—won't change with future Node.js releases
- Implies `--target es2023` (not `esnext`)
- Supports CommonJS requiring ESM modules
- Rejects deprecated import assertions

Use `node20` for predictable behavior; use `nodenext` to track latest Node.js features.

### Updated `tsc --init` Defaults

Generated `tsconfig.json` is now minimal with modern prescriptive defaults:

```json
{
  "compilerOptions": {
    "target": "esnext",
    "module": "nodenext",
    "strict": true,
    "moduleDetection": "force",
    "noUncheckedSideEffectImports": true,
    "noUncheckedIndexedAccess": true,
    "exactOptionalPropertyTypes": true
  }
}
```

Commented-out options removed in favor of editor autocompletion.

### Breaking Change: `ArrayBuffer` Type Narrowing

`ArrayBuffer` no longer supertypes `TypedArray` buffer variants. Code passing typed array buffers may need updates:

```typescript
// ❌ May error in 5.9
function process(buf: ArrayBuffer) { /* ... */ }
const arr = new Uint8Array(10);
process(arr); // Error: Uint8Array not assignable to ArrayBuffer

// ✅ Fix: access the underlying buffer
process(arr.buffer);

// ✅ Or: explicitly type the TypedArray
const arr = new Uint8Array<ArrayBuffer>(10);
```

Update `@types/node` if encountering `Buffer` compatibility issues.

### Breaking Change: Type Argument Inference

Changes to prevent type variable "leaks" during inference. Some code may require explicit generic type arguments:

```typescript
// Before: type inferred (possibly incorrectly)
const result = someGenericFunction(value);

// After: may need explicit type argument
const result = someGenericFunction<ExpectedType>(value);
```

---

## TypeScript 7 (Native Port)

TS7 rewrites the compiler in Go. TS6 is the last JS-based release.
**Details:** See [references/typescript-7-transition.md](references/typescript-7-transition.md)

Overview

This skill provides up-to-date guidance and quick fixes for TypeScript features and breaking changes introduced in TypeScript 5.9 and the TS7 transition. It highlights new syntax like import defer, the stable --module node20 option, updated tsc --init defaults, ArrayBuffer narrowing, and changes to type argument inference. Use it while writing or migrating TypeScript code to avoid common pitfalls and to apply concrete workarounds.

How this skill works

The skill inspects code patterns and compiler configurations to detect usage that may be affected by 5.9 changes. It explains allowed forms (for example, the restricted import defer namespace form), recommends compiler options (node20 vs nodenext), and suggests minimal code edits for breaking changes like ArrayBuffer narrowing and inference leaks. It also summarizes the new default tsconfig snippet and points to upgrade steps for related ambient types such as @types/node.

When to use it

  • When adding or reviewing code that imports modules lazily or using new import forms (import defer).
  • When choosing a stable Node.js module target for builds or tools (--module node20).
  • When running tsc --init and wanting the modern minimal default tsconfig.
  • When TypeScript errors appear after upgrading to 5.9 related to ArrayBuffer or typed arrays.
  • When generic type inference stops working and explicit type arguments may be required.
  • When preparing for the TS7 native port and assessing migration impact.

Best practices

  • Use import defer only as a namespace import: import defer * as name from './mod.js'.
  • Prefer --module node20 for stable, non-moving Node.js module semantics; use nodenext to follow latest Node behavior.
  • Adopt the new minimal tsconfig defaults and rely on editor autocompletion for optional flags.
  • When passing typed arrays to APIs expecting ArrayBuffer, use .buffer or update parameter types to accept TypedArray types.
  • Be explicit with generic type arguments when inference produces errors after upgrading.
  • Update related declaration packages (for example @types/node) when encountering platform type regressions.

Example use cases

  • Refactoring lazy-loaded feature modules to use import defer for zero-cost startup until an export is accessed.
  • Configuring a build for Node.js 20 behavior in a library or service using --module node20 for predictable output.
  • Regenerating tsconfig with tsc --init to get modern safe defaults and removing commented legacy options.
  • Fixing a compilation error where a function accepting ArrayBuffer no longer accepts a Uint8Array by switching to arr.buffer.
  • Resolving a broken generic helper by adding an explicit type parameter where type variable leaks were prevented.

FAQ

Can I use named imports with import defer?

No. import defer only supports namespace imports (import defer * as name). Named and default imports are not allowed.

When should I pick node20 vs nodenext?

Pick node20 for stable, unchanging Node.js-20 semantics. Use nodenext if you want the compiler to track the latest Node behavior.