TypeScript types for new JavaScript
This package, `new-javascript`, provides TypeScript type definitions for cutting-edge and experimental Web APIs and JavaScript features that are not yet incorporated into TypeScript's standard `dom` library or `@types/web`. As of version 0.4.7, it covers a wide range of specifications from groups like WICG, including the File System Access API, View Transitions, and Compression Streams. The project's release cadence is driven by the evolution of these web standards and TypeScript's integration schedule. A key differentiator is its proactive approach to providing types for early-stage APIs, using automated WebIDL-to-TypeScript conversion with manual refinement. It also uniquely includes worker-exclusive interfaces by default and offers specific type declarations for various worklet types through distinct import paths, helping developers target specific environments.
Common errors
-
Cannot find name 'FileSystemFileHandle'.
cause The type definitions for `new-javascript` are not correctly included in your project, or `tsconfig.json` is missing the necessary configuration.fixEnsure `new-javascript` is installed as a dev dependency (`npm i -D new-javascript`) and add `"new-javascript"` to the `compilerOptions.types` array in your `tsconfig.json`. Alternatively, add `/// <reference types="new-javascript" />` to the top of your TypeScript files. -
Property 'startViewTransition' does not exist on type 'Document'.
cause TypeScript's default DOM library (e.g., `lib.dom.d.ts`) does not yet include this specific Web API, and `new-javascript`'s types are not being loaded or are not correctly configured.fixVerify that `new-javascript` is included in your `tsconfig.json`'s `compilerOptions.types` array. Also, ensure your `compilerOptions.lib` includes `"dom"` and potentially `"dom.iterable"` and `"esnext"` to cover a broad range of browser APIs and modern JavaScript features. -
Duplicate identifier 'CompressionStream'.
cause This error occurs when `CompressionStream` (or another type provided by `new-javascript`) is defined in multiple places, most commonly by both `new-javascript` and a newer version of TypeScript's built-in `dom` library.fixCheck your TypeScript version; if it's recently updated, it might natively include the type. You may need to remove `new-javascript` if the official types are sufficient, or find a way to exclude the specific conflicting type definition from `new-javascript` (e.g., via `tsconfig.json` `files` or `exclude` if granular enough, or wait for an `new-javascript` update that defers to native types).
Warnings
- gotcha As this package provides type definitions for experimental or rapidly evolving Web APIs, there is a significant potential for future type conflicts or duplications once official definitions are integrated into TypeScript's standard `dom` library or other `@types` packages. This can lead to `Duplicate identifier` errors during compilation.
- gotcha This package includes type definitions for worker-exclusive interfaces (e.g., `FileReaderSync`, `FileSystemSyncAccessHandle`) by default. If your project is exclusively for the main thread, these types may incorrectly suggest the availability of APIs not present in that context, potentially leading to runtime errors if used.
- gotcha The APIs type-declared in `new-javascript` are often from WICG or other draft specifications, meaning their interfaces and behaviors are subject to change. This can lead to breaking type changes in future minor versions of `new-javascript` as it strives to align with the latest drafts, potentially requiring code adjustments without a major version bump of the package.
Install
-
npm install new-javascript -
yarn add new-javascript -
pnpm add new-javascript
Imports
- Global Web API types (e.g., FileSystemHandle)
import type { FileSystemHandle } from 'new-javascript';/// <reference types="new-javascript" />
- AudioWorklet Global Types
import type { AudioWorkletGlobalScope } from 'new-javascript/worklet/audio';/// <reference types="new-javascript/worklet/audio" />
- PaintWorklet Global Types
import type { PaintWorkletGlobalScope } from 'new-javascript/worklet/paint';/// <reference types="new-javascript/worklet/paint" />
Quickstart
npm i -D new-javascript@latest
// tsconfig.json
// {
// "compilerOptions": {
// "types": ["new-javascript"],
// "lib": ["dom", "dom.iterable", "esnext"]
// }
// }
// Example TypeScript file (e.g., src/main.ts)
/// <reference types="new-javascript" />
async function demonstrateNewApiUsage() {
// Using File System Access API types, like FileSystemFileHandle
if ('showOpenFilePicker' in window) {
try {
const [fileHandle] = await window.showOpenFilePicker();
console.log('File picked:', fileHandle.name);
const file = await fileHandle.getFile();
console.log('File size:', file.size, 'bytes');
// Additional operations with fileHandle or file
} catch (error) {
if (error instanceof DOMException && error.name === 'AbortError') {
console.log('File picker was dismissed.');
} else {
console.error('Error with File System Access API:', error);
}
}
} else {
console.warn('File System Access API not supported by this browser.');
}
// Using View Transitions API types, like document.startViewTransition
if (document.startViewTransition) {
console.log('View Transitions API is supported. Starting a transition.');
const transition = document.startViewTransition(() => {
// Perform DOM updates that trigger the transition
const contentDiv = document.getElementById('content-area');
if (contentDiv) {
contentDiv.innerHTML = `<h2>Content updated at ${new Date().toLocaleTimeString()}</h2><p>This is new content.</p>`;
}
return Promise.resolve();
});
try {
await transition.finished;
console.log('View transition completed successfully.');
} catch (error) {
console.error('View transition failed:', error);
}
} else {
console.warn('View Transitions API not supported by this browser.');
}
}
demonstrateNewApiUsage();