sveltejs/kit · warning

Using $lib/${file} instead of generated .d.ts file

Error message

Using $lib/${file} instead of generated .d.ts file

What it means

svelte-package generates .d.ts files into a temp directory and then copies them into the package, applying the $lib alias. If a generated declaration file collides with a handwritten .d.ts the user placed in the source tree (tracked in the `handwritten` set), it warns that the handwritten $lib/<file> version is being used instead of the generated one, so the generated types for that file are discarded.

Source

Thrown at packages/package/src/typescript.js:59

			? require.resolve('svelte2tsx/svelte-shims-v4.d.ts')
			: require.resolve('svelte2tsx/svelte-shims.d.ts'),
		declarationDir: tmp,
		tsconfig
	});

	const handwritten = new Set();

	// skip files that conflict with hand-written .d.ts
	for (const file of files) {
		if (file.name.endsWith('.d.ts')) {
			handwritten.add(file.name);
		}
	}

	// resolve $lib alias (TODO others), copy into package dir
	for (const file of walk(tmp)) {
		if (handwritten.has(file)) {
			console.warn(`Using $lib/${file} instead of generated .d.ts file`);
		}

		let source = fs.readFileSync(path.join(tmp, file), 'utf8');
		if (file.endsWith('.d.ts.map')) {
			// Because we put the .d.ts files in a temporary directory, the relative path needs to be adjusted
			const parsed = JSON.parse(source);
			if (parsed.sources) {
				parsed.sources = /** @type {string[]} */ (parsed.sources).map((source) =>
					posixify(
						path.join(
							path.relative(
								path.dirname(path.join(final_output, file)),
								path.dirname(path.join(input, file))
							),
							path.basename(source)
						)
					)
				);

View on GitHub (pinned to 03f1687fe6)

Solutions

  1. Delete the handwritten .d.ts file if it duplicates a .js/.ts source and let svelte-package generate types
  2. Rename or move the handwritten declarations to a non-colliding path
  3. Regenerate types and diff the generated output against the handwritten file to reconcile them

Example fix

// before: both src/lib/utils/index.js and src/lib/utils/index.d.ts exist
// after: delete src/lib/utils/index.d.ts and import types from the generated output
Defensive patterns

Strategy: validation

Validate before calling

import fs from 'node:fs';
const generated = 'src/lib/utils/index.d.ts';
if (fs.existsSync(generated) || fs.existsSync(generated.replace('.d.ts', '.js'))) {
  console.warn('handwritten .d.ts will shadow generated types');
}

Prevention

When it happens

Trigger: Running `svelte-package` (emit_dts) when a hand-authored .d.ts file in src/lib has the same output path as a file TypeScript would generate (e.g. src/lib/utils/index.d.ts alongside src/lib/utils/index.js).

Common situations: Manually maintained ambient/handwritten declarations left over after converting JS to TS; intentionally hand-written types that shadow generated ones after adding a matching .js/.ts file; stale .d.ts files committed to the repo before a build-tool migration.

Related errors


AI-assisted analysis of sveltejs/kit@03f1687fe6 (2026-09-02). Data as JSON: /api/errors/0eed2994ee7965b9. Report an issue: GitHub.