🤖 Merge PR #46472 [trusted-types] Fix issue with types that could not be named by @bjarkler

The new Trusted Types definitions introduced by #46321 contained an
unexported interface, which caused type checking errors when
trustedTypes.createPolicy was referenced, but not called. This fixes
that by removing the interface, and instead adding a type parameter to
the public TrustedTypePolicy interface.

Thanks to @engelsdamien for the help.
This commit is contained in:
bjarkler 2020-07-31 11:42:02 +00:00 committed by GitHub
parent 6ffddc7fd5
commit 7caf9e949e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 20 deletions

View File

@ -11,16 +11,6 @@ type FnNames = keyof TrustedTypePolicyOptions;
type Args<Options extends TrustedTypePolicyOptions, K extends FnNames> =
Parameters<NonNullable<Options[K]>>;
interface FullTypedPolicy<Options extends TrustedTypePolicyOptions> {
name: string;
createHTML(...args: Args<Options, 'createHTML'>): TrustedHTML;
createScript(...args: Args<Options, 'createScript'>): TrustedScript;
createScriptURL(...args: Args<Options, 'createScriptURL'>): TrustedScriptURL;
}
type TypedPolicy<Options extends TrustedTypePolicyOptions> =
Pick<FullTypedPolicy<Options>, 'name' | Extract<keyof Options, FnNames>>;
declare global {
class TrustedHTML {
private constructor(); // To prevent instantiting with 'new'.
@ -41,7 +31,7 @@ declare global {
createPolicy<Options extends TrustedTypePolicyOptions>(
policyName: string,
policyOptions?: Options,
): TypedPolicy<Options>;
): Pick<TrustedTypePolicy<Options>, 'name'|Extract<keyof Options, FnNames>>;
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<Options extends TrustedTypePolicyOptions = TrustedTypePolicyOptions> {
readonly name: string;
createHTML(input: string, ...arguments: any[]): TrustedHTML;
createScript(input: string, ...arguments: any[]): TrustedScript;
createScriptURL(input: string, ...arguments: any[]): TrustedScriptURL;
createHTML(...args: Args<Options, 'createHTML'>): TrustedHTML;
createScript(...args: Args<Options, 'createScript'>): TrustedScript;
createScriptURL(...args: Args<Options, 'createScriptURL'>): 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;

View File

@ -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;