Skip to content

Commit cb18026

Browse files
committed
Fetch TypeScript standard libs at the bundled compiler version
Standard lib typings (lib.dom.d.ts etc.) were fetched from the CDN at typescript@latest unless the project pinned a typescript version. Since TypeScript 7 was published, the latest package no longer contains those files, so the worker got no standard typings. That broke type checking and completions that rely on them, and the completions tests. Default to the version of the TypeScript compiler bundled in the worker instead, which is the version the lib files need to match anyway. A typescript version in the project's package.json still takes precedence.
1 parent 8c615a0 commit cb18026

4 files changed

Lines changed: 96 additions & 4 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,15 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1212

1313
<!-- ## [X.Y.Z] - YYYY-MM-DD -->
1414

15-
<!-- ## [Unreleased] -->
15+
## [Unreleased]
16+
17+
### Fixed
18+
19+
- TypeScript standard library typings (e.g. `lib.dom.d.ts`) are now fetched at
20+
the version of the TypeScript compiler bundled with Playground, instead of at
21+
`typescript@latest`. They could no longer be found since TypeScript 7 was
22+
published, which broke type checking and completions that rely on them. A
23+
`typescript` version in the project's `package.json` still takes precedence.
1624

1725
## [0.21.2] - 2025-10-02
1826

‎src/test/types-fetcher_test.ts‎

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
import {TypesFetcher} from '../typescript-worker/types-fetcher.js';
8+
import ts from '../internal/typescript.js';
89
import {ImportMapResolver} from '../typescript-worker/import-map-resolver.js';
910
import {configureFakeCdn} from './worker-test-util.js';
1011
import {assert} from '@esm-bundle/chai';
@@ -571,10 +572,78 @@ suite('types fetcher', () => {
571572
});
572573
});
573574

574-
test('tslibs', async () => {
575+
test('tslibs default to the bundled TypeScript version', async () => {
575576
const sourceTexts: string[] = [];
576577
const tsLibs = ['es2020', 'DOM'];
577578
const packageJson: PackageJson = {};
579+
const cdnData: CdnData = {
580+
typescript: {
581+
versions: {
582+
[ts.version]: {
583+
files: {
584+
'lib/lib.es2020.d.ts': {
585+
// References are the same as imports from our perspective.
586+
content: `/// <reference lib="es2020.promise" />`,
587+
},
588+
'lib/lib.es2020.promise.d.ts': {
589+
content: `interface PromiseConstructor { }`,
590+
},
591+
// Note standard lib files are always lower-case.
592+
'lib/lib.dom.d.ts': {
593+
content: `interface HTMLElement { }`,
594+
},
595+
},
596+
},
597+
// TypeScript 7 doesn't have the lib files. We must not try to load
598+
// the libs from here just because it is the latest version.
599+
'7.0.0': {
600+
files: {},
601+
},
602+
},
603+
},
604+
};
605+
const expectedDependencyGraph: ExpectedDependencyGraph = {
606+
root: {
607+
typescript: ts.version,
608+
},
609+
deps: {},
610+
};
611+
const expectedLayout: NodeModulesDirectory = {
612+
typescript: {
613+
version: ts.version,
614+
nodeModules: {},
615+
},
616+
};
617+
const expectedFiles = new Map([
618+
[
619+
'typescript/lib/lib.es2020.d.ts',
620+
`/// <reference lib="es2020.promise" />`,
621+
],
622+
[
623+
'typescript/lib/lib.es2020.promise.d.ts',
624+
`interface PromiseConstructor { }`,
625+
],
626+
['typescript/lib/lib.dom.d.ts', `interface HTMLElement { }`],
627+
]);
628+
await checkTypesFetcher({
629+
sourceTexts,
630+
tsLibs,
631+
packageJson,
632+
cdnData,
633+
expectedFiles,
634+
expectedDependencyGraph,
635+
expectedLayout,
636+
});
637+
});
638+
639+
test('tslibs use the TypeScript version from package.json', async () => {
640+
const sourceTexts: string[] = [];
641+
const tsLibs = ['es2020', 'DOM'];
642+
const packageJson: PackageJson = {
643+
dependencies: {
644+
typescript: '4.3.5',
645+
},
646+
};
578647
const cdnData: CdnData = {
579648
typescript: {
580649
versions: {

‎src/test/typescript-worker_test.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
import {checkTransform} from './worker-test-util.js';
8+
import ts from '../internal/typescript.js';
89

910
import {BuildOutput, SampleFile} from '../shared/worker-api.js';
1011
import {CdnData} from './fake-cdn-plugin.js';
@@ -529,7 +530,8 @@ suite('typescript builder', () => {
529530
},
530531
typescript: {
531532
versions: {
532-
'4.3.5': {
533+
// Standard libs are fetched at the version of the bundled compiler.
534+
[ts.version]: {
533535
files: {
534536
'lib/lib.dom.d.ts': {
535537
content: '',

‎src/typescript-worker/types-fetcher.ts‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,20 @@ export class TypesFetcher {
264264
from === root
265265
? this._rootPackageJson
266266
: await this._fetchPackageJsonAndAddToOutputFiles(from);
267-
return packageJson?.dependencies?.[to] ?? 'latest';
267+
const version = packageJson?.dependencies?.[to];
268+
if (version !== undefined) {
269+
return version;
270+
}
271+
if (from === root && to === 'typescript') {
272+
// The project didn't pick a TypeScript version, so use the version of
273+
// the compiler we are running. This is what the standard lib typings
274+
// (lib.dom.d.ts etc.) are fetched at, and they need to match our
275+
// compiler. We can't use "latest", because newer major versions of the
276+
// "typescript" package might not be compatible, or might not contain
277+
// these files at all (TypeScript 7 does not).
278+
return ts.version;
279+
}
280+
return 'latest';
268281
}
269282

270283
private async _getDtsPath(location: NpmFileLocation): Promise<string> {

0 commit comments

Comments
 (0)