Skip to content

Commit 4d28559

Browse files
Copilotnev21Copilot
authored
Fix ES2015 built-in type errors in consumers by adding lib reference directive to published declarations (#558)
Consumers using `"lib": ["ES5", "DOM"]` (or omitting `lib` entirely) get TypeScript errors like `Cannot find name 'Symbol'` / `Cannot find name 'Iterator'` because the package's published `.d.ts` file references ES2015 built-ins without declaring that dependency to TypeScript. ## Root Cause The bundled declaration file (`lib/dist/types/ts-utils.d.ts`) exposes ES2015 types such as `Symbol`, `Iterator<T>`, `Iterable<T>`, `IterableIterator<T>`, and `Promise<T>` in its public API surface. Without a `/// <reference lib="es2015" />` directive in the published file, consumers must manually add `"lib": ["ES2015"]` to their own `tsconfig.json` — otherwise TypeScript cannot resolve these types. ## Changes - **`lib/src/index.ts`** and **`lib/src/polyfills.ts`** — adds `/// <reference lib="es2015" />` to the source entry points so TypeScript includes the directive in generated `.d.ts` files and validates against ES2015 types at build time - **`lib/scripts/setTsReferences.js`** — new post-processing script that prepends `/// <reference lib="es2015" />` to the api-extractor bundled output (`lib/dist/types/ts-utils.d.ts`); this is required because api-extractor strips `/// <reference lib="..." />` directives from its rollup - **`package.json`** — updates the `dtsgen` script to `api-extractor run --local --verbose && node lib/scripts/setTsReferences.js` so the post-processing runs automatically as part of every build - **`lib/tsconfig.base.json`** — adds `"lib": ["ES2015", "DOM"]`; propagates to both the ES5 and ES6 build configs via `extends` - **`lib/test/tsconfig.test.json`**, **`lib/test/tsconfig.test.karma.json`**, **`lib/test/tsconfig.worker.karma.json`** — add `"lib": ["ES2015", "DOM"]` - **`lib/test/tsconfig.test.esnext.json`** — adds `"lib": ["ESNext", "DOM"]` - **`CHANGELOG.md`** — adds bug fix entry for v0.13.0 The net effect is that consumers who import from `@nevware21/ts-utils` automatically get the ES2015 type definitions included by TypeScript — no change to their own `tsconfig.json` is required. `DOM` is included alongside `ES2015` in all tsconfig files because the public API exports types like `Window`, `Document`, `Navigator`, and `History` (from `helpers/environment.ts`), which require the DOM lib to resolve. This does not change the JavaScript runtime target or emit. --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: nev21 <82737406+nev21@users.noreply.github.com> Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
1 parent da838a4 commit 4d28559

10 files changed

Lines changed: 58 additions & 1 deletion

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
## Changelog
44

5+
### Bug Fixes
6+
7+
- [#565](https://github.com/nevware21/ts-utils/issues/565) Fixed compilation errors for consumers using ES5-only TypeScript lib settings.
8+
- Added `/// <reference lib="es2015" />` directive to the published `.d.ts` declaration file so that consumers with `"lib": ["ES5", "DOM"]` (or no explicit `lib`) can compile without "Cannot find name 'Symbol'" / "Cannot find name 'Iterator'" errors.
9+
- The fix includes a post-processing step (`lib/scripts/setTsReferences.js`) that adds the reference directive to the api-extractor bundled output, since api-extractor strips `/// <reference lib="..." />` from its rollup.
10+
- Also added `"lib": ["ES2015", "DOM"]` to the library build and test tsconfig files for consistent compile-time validation.
11+
512
### Features
613

714
- Added comprehensive encoding and decoding functions for multiple formats:

lib/scripts/setTsReferences.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* @nevware21/ts-utils
3+
* https://github.com/nevware21/ts-utils
4+
*
5+
* Copyright (c) 2022-2026 NevWare21 Solutions LLC
6+
* Licensed under the MIT license.
7+
*/
8+
9+
"use strict";
10+
11+
/**
12+
* Post-processing script to add TypeScript lib reference directives to the bundled
13+
* declaration file generated by api-extractor.
14+
*
15+
* The api-extractor tool strips `/// <reference lib="..." />` directives from the
16+
* bundled output. These directives are required so that consumers using ES5-only lib
17+
* settings in their tsconfig (e.g. `"lib": ["ES5", "DOM"]`) can compile code that
18+
* references the package's type declarations without encountering "Cannot find name"
19+
* errors for ES2015 built-ins such as Symbol, Iterator, Iterable, etc.
20+
*/
21+
22+
var fs = require("fs");
23+
var path = require("path");
24+
25+
var bundledDtsFile = path.resolve(__dirname, "../dist/types/ts-utils.d.ts");
26+
var referenceDirective = "/// <reference lib=\"es2015\" />";
27+
28+
if (!fs.existsSync(bundledDtsFile)) {
29+
console.error("ERROR: Bundled d.ts file not found: " + bundledDtsFile);
30+
process.exit(1);
31+
}
32+
33+
var content = fs.readFileSync(bundledDtsFile, "utf8");
34+
var newLine = content.indexOf("\r\n") !== -1 ? "\r\n" : "\n";
35+
36+
if (content.indexOf(referenceDirective) === -1) {
37+
fs.writeFileSync(bundledDtsFile, referenceDirective + newLine + content, "utf8");
38+
console.log("Added '" + referenceDirective + "' to " + path.relative(process.cwd(), bundledDtsFile));
39+
} else {
40+
console.log("Reference directive already present in " + path.relative(process.cwd(), bundledDtsFile));
41+
}

lib/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
* Licensed under the MIT license.
77
*/
88

9+
/// <reference lib="es2015" />
10+
911
export { arrAppend } from "./array/append";
1012
export { ArrPredicateCallbackFn, ArrPredicateCallbackFn2, ArrMapCallbackFn, ArrFlatMapCallbackFn, ArrFromMapFn } from "./array/callbacks";
1113
export { arrAt } from "./array/at";

lib/src/polyfills.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
* Licensed under the MIT license.
77
*/
88

9+
/// <reference lib="es2015" />
10+
911
import { arrForEach } from "./array/forEach";
1012
import { ArrCls, ArrProto, ObjClass, StrProto } from "./internal/constants";
1113
import { polyIsArray, polyArrIncludes, polyArrFind, polyArrFindIndex, polyArrFindLastIndex, polyArrFindLast, polyArrFrom } from "./polyfills/array";

lib/test/tsconfig.test.esnext.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"module": "commonjs",
77
"moduleResolution": "node",
88
"target": "ESNext",
9+
"lib": ["ESNext", "DOM"],
910
"forceConsistentCasingInFileNames": true,
1011
"importHelpers": true,
1112
"noEmitHelpers": false,

lib/test/tsconfig.test.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"module": "commonjs",
77
"moduleResolution": "node",
88
"target": "es5",
9+
"lib": ["ES2015", "DOM"],
910
"forceConsistentCasingInFileNames": true,
1011
"importHelpers": true,
1112
"noEmitHelpers": false,

lib/test/tsconfig.test.karma.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"module": "commonjs",
77
"moduleResolution": "node",
88
"target": "es5",
9+
"lib": ["ES2015", "DOM"],
910
"forceConsistentCasingInFileNames": true,
1011
"importHelpers": true,
1112
"noEmitHelpers": false,

lib/test/tsconfig.worker.karma.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"module": "es6",
77
"moduleResolution": "node",
88
"target": "es5",
9+
"lib": ["ES2015", "DOM"],
910
"forceConsistentCasingInFileNames": true,
1011
"importHelpers": true,
1112
"noEmitHelpers": false,

lib/tsconfig.base.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"module": "es6",
77
"moduleResolution": "node",
88
"target": "es5",
9+
"lib": ["ES2015", "DOM"],
910
"forceConsistentCasingInFileNames": true,
1011
"importHelpers": true,
1112
"noEmitHelpers": false,

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@
185185
"prep-release": "npm run cleanBuild && npm run npm-pack",
186186
"npm-pack": "copyfiles README.md LICENSE lib && cd lib && npm pack",
187187
"npm-publish": "publish-npm",
188-
"dtsgen": "api-extractor run --local --verbose",
188+
"dtsgen": "api-extractor run --local --verbose && node lib/scripts/setTsReferences.js",
189189
"size": "size-limit",
190190
"size-save": "size-limit --clean-dir --save-bundle build/size",
191191
"size-check": "node lib/test/bundle-size-check.js",

0 commit comments

Comments
 (0)