TypeScript types for new JavaScript

0.4.7 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

Demonstrates how to install and configure `new-javascript` types via `tsconfig.json` and a reference directive, then utilizes types from the File System Access API and View Transitions API in a runnable TypeScript example.

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();

view raw JSON →