Skip to content

Commit e217615

Browse files
Brooooooklynclaude
andauthored
Add custom dts headers doc (#419)
* Add custom dts headers doc * fix: address PR review comments - Fix dtsHeader documentation to correctly state it replaces (not appends to) the default header - Remove auto-generated routes.d.ts reference from next-env.d.ts that breaks fresh checkouts 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
1 parent c4755c7 commit e217615

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed

pages/docs/concepts/types-overwrite.en.md

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,112 @@ export interface TsTypeChanged {
106106
typeOverrideOptional?: object
107107
}
108108
```
109+
110+
## Custom Type Definitions in Header
111+
112+
When NAPI-RS generates `index.d.ts`, it includes a default header. You can customize this header to add your own TypeScript types, imports, or comments that your native module needs.
113+
114+
### Use Cases
115+
116+
- **Custom type aliases**: Define types like `MaybePromise<T>` used by your API
117+
- **Import external types**: Import `ReadableStream`, `Buffer`, or other Node.js types
118+
- **ESLint/TypeScript directives**: Add `// @ts-nocheck` or custom rules
119+
- **Documentation**: Copyright notices, version info, or deprecation warnings
120+
- **Declare symbols**: Export constants or symbols used by your bindings
121+
122+
### Configuration Options
123+
124+
| Method | Location | Best For |
125+
| ----------------- | ----------- | ---------------------------- |
126+
| `dtsHeaderFile` | napi config | Complex headers with imports |
127+
| `dtsHeader` | napi config | Simple single-line additions |
128+
| `--dts-header` | CLI flag | CI/CD overrides |
129+
| `--no-dts-header` | CLI flag | Disable header entirely |
130+
131+
### Priority Resolution
132+
133+
When multiple options are set, NAPI-RS resolves them in this order:
134+
135+
| Priority | Source | Description |
136+
| :------: | ------------------------ | --------------------------------------------------- |
137+
| 1 | `dtsHeaderFile` (config) | File path in `napi` config - **always wins if set** |
138+
| 2 | `--dts-header` (CLI) | CLI flag overrides inline config |
139+
| 3 | `dtsHeader` (config) | Inline string in `napi` config |
140+
| 4 | Default header | Used when nothing else is specified |
141+
142+
> **Key point**: `dtsHeaderFile` in config takes precedence over ALL other options, including the `--dts-header` CLI flag. If you need CLI override capability, use `dtsHeader` instead of `dtsHeaderFile`.
143+
144+
### Example Scenarios
145+
146+
| Config `dtsHeaderFile` | Config `dtsHeader` | CLI `--dts-header` | Result |
147+
| :--------------------: | :----------------: | :----------------: | -------------------- |
148+
| `./header.d.ts` | `"type X = Y"` | `"// override"` | Uses `./header.d.ts` |
149+
| - | `"type X = Y"` | `"// override"` | Uses `"// override"` |
150+
| - | `"type X = Y"` | - | Uses `"type X = Y"` |
151+
| - | - | - | Uses default header |
152+
153+
### Using `dtsHeaderFile` (Recommended)
154+
155+
Create a separate `.d.ts` file for complex headers:
156+
157+
**Step 1: Create the header file**
158+
159+
```typescript filename="dts-header.d.ts"
160+
/* auto-generated by NAPI-RS */
161+
/* eslint-disable */
162+
163+
import type { ReadableStream } from 'node:stream/web'
164+
165+
type MaybePromise<T> = T | Promise<T>
166+
167+
export declare const MY_SYMBOL: symbol
168+
```
169+
170+
**Step 2: Reference it in package.json**
171+
172+
```json filename="package.json"
173+
{
174+
"napi": {
175+
"dtsHeaderFile": "./dts-header.d.ts"
176+
}
177+
}
178+
```
179+
180+
> ⚠️ The file content **completely replaces** the default header. Include the auto-generated comment and eslint directive if you want to keep them.
181+
182+
### Using `dtsHeader` (Inline)
183+
184+
For simple additions, use an inline string in your config:
185+
186+
```json filename="package.json"
187+
{
188+
"napi": {
189+
"dtsHeader": "type MaybePromise<T> = T | Promise<T>"
190+
}
191+
}
192+
```
193+
194+
This completely replaces the default header. Include the auto-generated comment and eslint directive in the string if you want to keep them.
195+
196+
### CLI Options
197+
198+
**`--dts-header`**: Override the header via CLI (useful for CI/CD):
199+
200+
```sh
201+
napi build --dts-header "// Custom header"
202+
```
203+
204+
**`--no-dts-header`**: Generate `.d.ts` without any header:
205+
206+
```sh
207+
napi build --no-dts-header
208+
```
209+
210+
### Default Header
211+
212+
Without customization, NAPI-RS uses:
213+
214+
```typescript
215+
/* auto-generated by NAPI-RS */
216+
/* eslint-disable */
217+
```

0 commit comments

Comments
 (0)