What is @types/trusted-types?
The @types/trusted-types npm package provides TypeScript type definitions for the Trusted Types API, which is a web security standard aimed at preventing Cross-Site Scripting (XSS) by restricting access to potentially dangerous DOM APIs. These type definitions allow developers to use Trusted Types in TypeScript projects with type checking and IntelliSense support.
Type Definitions for Trusted Types
This feature provides TypeScript type definitions for creating and manipulating Trusted Types, such as TrustedHTML, TrustedScript, and TrustedURL. The code sample demonstrates how to create a TrustedHTML type using a Trusted Types policy.
import { TrustedHTML, TrustedScriptURL } from 'trusted-types';
function createTrustedTypes(policyName: string): TrustedHTML {
const policy = trustedTypes.createPolicy(policyName, {
createHTML: (input) => input
});
return policy.createHTML('<div>Safe Content</div>');
}