{"library":"navigation-api-types","title":"Type Definitions for Navigation API","description":"This package provides comprehensive TypeScript type definitions for the W3C Navigation API, accessible via `window.navigation` in modern browsers. It is a types-only package, meaning it does not include a polyfill or runtime implementation of the API, focusing solely on providing accurate typings for development. The current stable version is 0.6.1, with releases occurring periodically to align with updates and changes in the Navigation API specification. Its primary differentiator is offering early access to these types, enabling developers to leverage the Navigation API in TypeScript projects before broader `lib.dom` support or for specific browser targetting, ensuring type safety and improved developer experience.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install navigation-api-types"],"cli":null},"imports":["/// <reference types=\"navigation-api-types\" />\n// (Place this in a global .d.ts file, e.g., src/global.d.ts)","/// <reference types=\"navigation-api-types\" />\n// (Place this at the top of a .ts file)","declare const event: NavigationEvent;"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import './src/global.d.ts'; // Ensure your global type definitions are included (or adjust path)\n\n// src/global.d.ts (or similar file in your project):\n// /// <reference types=\"navigation-api-types\" />\n\n// --- main.ts (or any application file) ---\n\nfunction setupNavigationListener() {\n  if ('navigation' in window) {\n    console.log('Navigation API is supported.');\n    \n    // 'navigation' property now has types due to the reference directive\n    window.navigation.addEventListener('navigate', (event: NavigateEvent) => {\n      console.log(`Navigation event caught: ${event.destination.url}`);\n      if (event.destination.url.includes('/sensitive')) {\n        event.intercept({\n          async handler() {\n            console.log('Intercepting navigation to sensitive area.');\n            // Simulate some async work, e.g., auth check\n            await new Promise(resolve => setTimeout(resolve, 500));\n            console.log('Navigation intercepted and handled.');\n            // You might redirect or prevent default here\n          }\n        });\n      }\n    });\n    console.log('Navigation event listener added.');\n  } else {\n    console.warn('Navigation API is not supported in this browser.');\n  }\n}\n\nsetupNavigationListener();\n\n// Example of directly accessing a typed API property\nif ('navigation' in window && window.navigation.currentEntry) {\n  console.log('Current navigation entry ID:', window.navigation.currentEntry.id);\n}\n\n// To make this runnable, ensure a tsconfig.json is set up to include global.d.ts\n// and compile with tsc.","lang":"typescript","description":"This example demonstrates how to set up the global ambient types for the Navigation API and then use `window.navigation` with full type safety to add event listeners and access properties, preventing TypeScript errors.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}