From 098400cd09ceb54a48d003ce8dbd258f55a3862e Mon Sep 17 00:00:00 2001 From: Sebastian Silbermann Date: Tue, 14 Feb 2023 22:13:02 +0100 Subject: [PATCH] =?UTF-8?q?[trusted-types]=20Properly=20augment=20global?= =?UTF-8?q?=20to=20prevent=20conflicts=20with=20lib=E2=80=A6=20(#64360)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * [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, https://github.com/DefinitelyTyped/DefinitelyTyped/pull/60691/ had to be reverted. With proper module augmentation we can land https://github.com/DefinitelyTyped/DefinitelyTyped/pull/60691/ again. * $ExpectError -> @ts-expect-error --- types/trusted-types/index.d.ts | 26 ++++++++++++++------------ types/trusted-types/test/browser.ts | 6 +++--- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/types/trusted-types/index.d.ts b/types/trusted-types/index.d.ts index 6c3d7eaf0a1764..746bab2f641ce4 100644 --- a/types/trusted-types/index.d.ts +++ b/types/trusted-types/index.d.ts @@ -4,6 +4,7 @@ // Damien Engels // Emanuel Tesar // Bjarki +// Sebastian Silbermann // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 3.1 @@ -11,23 +12,24 @@ 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 {} } diff --git a/types/trusted-types/test/browser.ts b/types/trusted-types/test/browser.ts index e3203d2cc99789..97d3dc897f51de 100644 --- a/types/trusted-types/test/browser.ts +++ b/types/trusted-types/test/browser.ts @@ -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