Skip to content

Commit

Permalink
[trusted-types] Properly augment global to prevent conflicts with lib… (
Browse files Browse the repository at this point in the history
DefinitelyTyped#64360)

* [trusted-types] Properly augment global to prevent conflicts with lib.dom.d.ts et.all

This is the proper patern to augment global interfaces that are defined by the runtime.

The previous approach would break once `lib.dom.d.ts` declares these interfaces.

The previous approach also made it impossible for other libraries to use these types.
For example, DefinitelyTyped#60691 had to be reverted.
With proper module augmentation we can land DefinitelyTyped#60691 again.

* $ExpectError -> @ts-expect-error
  • Loading branch information
Sebastian Silbermann authored and miccehedin committed Feb 15, 2023
1 parent 27b963c commit 098400c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
26 changes: 14 additions & 12 deletions types/trusted-types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,32 @@
// Damien Engels <https://github.com/engelsdamien>
// Emanuel Tesar <https://github.com/siegrift>
// Bjarki <https://github.com/bjarkler>
// Sebastian Silbermann <https://github.com/eps1lon>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 3.1

import * as lib from './lib';

// Re-export the type definitions globally.
declare global {
const TrustedHTML: typeof lib.TrustedHTML;
type TrustedHTML = lib.TrustedHTML;
const TrustedScript: typeof lib.TrustedScript;
type TrustedScript = lib.TrustedScript;
const TrustedScriptURL: typeof lib.TrustedScriptURL;
type TrustedScriptURL = lib.TrustedScriptURL;
// tslint:disable-next-line no-empty-interface -- interface to allow module augmentation
interface TrustedHTML extends lib.TrustedHTML {}
// tslint:disable-next-line no-empty-interface -- interface to allow module augmentation
interface TrustedScript extends lib.TrustedScript {}
// tslint:disable-next-line no-empty-interface -- interface to allow module augmentation
interface TrustedScriptURL extends lib.TrustedScriptURL {}

const TrustedTypePolicy: typeof lib.TrustedTypePolicy;
type TrustedTypePolicy = lib.TrustedTypePolicy;
// tslint:disable-next-line no-empty-interface -- interface to allow module augmentation
interface TrustedTypePolicy extends lib.TrustedTypePolicy {}

const TrustedTypePolicyFactory: typeof lib.TrustedTypePolicyFactory;
type TrustedTypePolicyFactory = lib.TrustedTypePolicyFactory;
// tslint:disable-next-line no-empty-interface -- interface to allow module augmentation
interface TrustedTypePolicyFactory extends lib.TrustedTypePolicyFactory {}

type TrustedTypePolicyOptions = lib.TrustedTypePolicyOptions;
// tslint:disable-next-line no-empty-interface -- interface to allow module augmentation
interface TrustedTypePolicyOptions extends lib.TrustedTypePolicyOptions {}

// Attach the relevant Trusted Types properties to the Window object.
// tslint:disable-next-line no-empty-interface
// tslint:disable-next-line no-empty-interface -- interface to allow module augmentation
interface Window extends lib.TrustedTypesWindow {}
}

Expand Down
6 changes: 3 additions & 3 deletions types/trusted-types/test/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,11 @@ trustedHTML = trustedScript;
// @ts-expect-error
new TrustedHTML();

// $ExpectType typeof TrustedHTML
// @ts-expect-error
TrustedHTML;
// $ExpectType typeof TrustedScript
// @ts-expect-error
TrustedScript;
// $ExpectType typeof TrustedScriptURL
// @ts-expect-error
TrustedScriptURL;

// $ExpectType typeof TrustedHTML
Expand Down

0 comments on commit 098400c

Please sign in to comment.