mirror of
https://github.com/FlipsideCrypto/DefinitelyTyped.git
synced 2026-02-06 10:56:53 +00:00
🤖 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:
parent
6ffddc7fd5
commit
7caf9e949e
27
types/trusted-types/index.d.ts
vendored
27
types/trusted-types/index.d.ts
vendored
@ -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;
|
||||
|
||||
@ -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;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user