diff --git a/types/trusted-types/index.d.ts b/types/trusted-types/index.d.ts index 04399d2ac3..a2d9fecf31 100644 --- a/types/trusted-types/index.d.ts +++ b/types/trusted-types/index.d.ts @@ -11,16 +11,6 @@ type FnNames = keyof TrustedTypePolicyOptions; type Args = Parameters>; -interface FullTypedPolicy { - name: string; - createHTML(...args: Args): TrustedHTML; - createScript(...args: Args): TrustedScript; - createScriptURL(...args: Args): TrustedScriptURL; -} - -type TypedPolicy = - Pick, 'name' | Extract>; - declare global { class TrustedHTML { private constructor(); // To prevent instantiting with 'new'. @@ -41,7 +31,7 @@ declare global { createPolicy( policyName: string, policyOptions?: Options, - ): TypedPolicy; + ): Pick, 'name'|Extract>; isHTML(value: unknown): value is TrustedHTML; isScript(value: unknown): value is TrustedScript; isScriptURL(value: unknown): value is TrustedScriptURL; @@ -52,11 +42,11 @@ declare global { readonly defaultPolicy: TrustedTypePolicy | null; } - interface TrustedTypePolicy { + interface TrustedTypePolicy { readonly name: string; - createHTML(input: string, ...arguments: any[]): TrustedHTML; - createScript(input: string, ...arguments: any[]): TrustedScript; - createScriptURL(input: string, ...arguments: any[]): TrustedScriptURL; + createHTML(...args: Args): TrustedHTML; + createScript(...args: Args): TrustedScript; + createScriptURL(...args: Args): TrustedScriptURL; } interface TrustedTypePolicyOptions { @@ -66,10 +56,9 @@ declare global { } interface Window { - // NOTE: This is needed while the implementation in Chrome still relies - // on the old uppercase name, either of the values below could be - // undefined. - // See https://github.com/w3c/webappsec-trusted-types/issues/177 + // `trustedTypes` is left intentionally optional to make sure that + // people handle the case when their code is running in a browser not + // supporting trustedTypes. trustedTypes?: TrustedTypePolicyFactory; TrustedHTML: TrustedHTML; TrustedScript: TrustedScript; diff --git a/types/trusted-types/trusted-types-tests.ts b/types/trusted-types/trusted-types-tests.ts index 3df99e8183..d146066190 100644 --- a/types/trusted-types/trusted-types-tests.ts +++ b/types/trusted-types/trusted-types-tests.ts @@ -15,7 +15,8 @@ const rules = { createScriptURL: (s: string) => s, }; -const policy = TT.createPolicy('test', rules); +const createPolicy = TT.createPolicy; +const policy = createPolicy('test', rules); // $ExpectType string policy.name; @@ -83,6 +84,19 @@ genericPolicy2.createScript('', true, {}); // $ExpectType TrustedScriptURL genericPolicy2.createScriptURL('', true, {}); +const castedAsGenericPolicy: TrustedTypePolicy = TT.createPolicy('castedAsGeneric', { + createHTML: (val: string, option1: number, option2: boolean) => val, + createScriptURL: (val: string) => val, + createScript: (val: string) => val, +}); + +// $ExpectType TrustedHTML +castedAsGenericPolicy.createHTML('', true, {}); +// $ExpectType TrustedScript +castedAsGenericPolicy.createScript('', true, {}); +// $ExpectType TrustedScriptURL +castedAsGenericPolicy.createScriptURL('', true, {}); + // Ensure the types are globally available let trustedHTML: TrustedHTML = null as any; const trustedScript: TrustedScript = null as any;