From 3962e8926fb60e58e68e3951c9d191bf260abcf8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emanuel=20Tesa=C5=99?= Date: Tue, 24 Sep 2019 23:16:48 +0200 Subject: [PATCH] Expose Trusted Types names in global scope (#38312) --- types/trusted-types/index.d.ts | 108 +++++++++++---------- types/trusted-types/trusted-types-tests.ts | 13 +++ 2 files changed, 69 insertions(+), 52 deletions(-) diff --git a/types/trusted-types/index.d.ts b/types/trusted-types/index.d.ts index 6de9054b42..dc2da3d51c 100644 --- a/types/trusted-types/index.d.ts +++ b/types/trusted-types/index.d.ts @@ -6,67 +6,71 @@ // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.1 -declare class TrustedHTML { - private readonly _brand: true; // To prevent structural typing. -} - -declare class TrustedScript { - private readonly _brand: true; // To prevent structural typing. -} - -declare class TrustedScriptURL { - private readonly _brand: true; // To prevent structural typing. -} - -declare class TrustedURL { - private readonly _brand: true; // To prevent structural typing. -} - -declare class TrustedTypePolicy { - readonly name: string; - createHTML(input: string): TrustedHTML; - createScript(input: string): TrustedScript; - createScriptURL(input: string): TrustedScriptURL; - createURL(input: string): TrustedURL; -} - -interface TrustedTypePolicyOptions { - createHTML?: (input: string) => string; - createScript?: (input: string) => string; - createScriptURL?: (input: string) => string; - createURL?: (input: string) => string; -} - -declare class TrustedTypePolicyFactory { - createPolicy( - name: string, - policyOptions: Pick, - expose?: boolean, - ): Pick; - getPolicyNames(): string[]; - - isHTML(value: any): value is TrustedHTML; - isScript(value: any): value is TrustedScript; - isScriptURL(value: any): value is TrustedScriptURL; - isURL(value: any): value is TrustedURL; - getAttributeType(tagName: string, attrName: string, elemNs?: string, attrNs?: string): string|null; - getPropertyType(tagName: string, propName: string, elemNs?: string): string|null; - defaultPolicy?: TrustedTypePolicy; - emptyHTML: TrustedHTML; -} - -declare const trustedTypes: TrustedTypePolicyFactory; - declare global { + interface TrustedTypePolicyOptions { + createHTML?: (input: string) => string; + createScript?: (input: string) => string; + createScriptURL?: (input: string) => string; + createURL?: (input: string) => string; + } + + class TrustedHTML { + private constructor(); // to prevent instantiting with 'new'. + private _brand: true; // To prevent structural typing. + } + + class TrustedScript { + private constructor(); // to prevent instantiting with 'new'. + private _brand: true; // To prevent structural typing. + } + + class TrustedScriptURL { + private constructor(); // to prevent instantiting with 'new'. + private _brand: true; // To prevent structural typing. + } + + class TrustedURL { + private constructor(); // to prevent instantiting with 'new'. + private _brand: true; // To prevent structural typing. + } + + interface TrustedTypePolicy { + readonly name: string; + createHTML(input: string): TrustedHTML; + createScript(input: string): TrustedScript; + createScriptURL(input: string): TrustedScriptURL; + createURL(input: string): TrustedURL; + } + + interface TrustedTypePolicyFactory { + createPolicy( + name: string, + policyOptions: Pick, + expose?: boolean, + ): Pick; + getPolicyNames(): string[]; + isHTML(value: any): value is TrustedHTML; + isScript(value: any): value is TrustedScript; + isScriptURL(value: any): value is TrustedScriptURL; + isURL(value: any): value is TrustedURL; + getAttributeType(tagName: string, attrName: string, elemNs?: string, attrNs?: string): string | undefined; + getPropertyType(tagName: string, propName: string, elemNs?: string): string | undefined; + defaultPolicy?: TrustedTypePolicy; + emptyHTML: TrustedHTML; + } + interface Window { trustedTypes: TrustedTypePolicyFactory; TrustedHTML: TrustedHTML; TrustedScript: TrustedScript; TrustedScriptURL: TrustedScriptURL; TrustedURL: TrustedURL; - TrustedTypePolicyOptions: TrustedTypePolicyOptions; TrustedTypePolicyFactory: TrustedTypePolicyFactory; + TrustedTypePolicy: TrustedTypePolicy; } } +// this is not available in global scope. It's only used for the export. +declare const trustedTypes: TrustedTypePolicyFactory; + export default trustedTypes; diff --git a/types/trusted-types/trusted-types-tests.ts b/types/trusted-types/trusted-types-tests.ts index 81931de820..ae3d8d3b04 100644 --- a/types/trusted-types/trusted-types-tests.ts +++ b/types/trusted-types/trusted-types-tests.ts @@ -3,6 +3,9 @@ import TT from 'trusted-types'; // $ExpectType TrustedTypePolicyFactory TT; +// $ExpectError +trustedTypes; + const rules = { createHTML: (s: string) => s, createScript: (s: string) => s, @@ -48,3 +51,13 @@ window.trustedTypes.isScript(html); window.trustedTypes.isScriptURL(html); // $ExpectType boolean window.trustedTypes.isURL(html); + +// test that types are globaly available +const trustedHTML: TrustedHTML = null as any; +const trustedScript: TrustedScript = null as any; + +// $ExpectError +trustedHTML = trustedScript; + +// $ExpectError +new TrustedHTML();