mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-10-04 23:07:06 +00:00
Expose Trusted Types names in global scope (#38312)
This commit is contained in:
committed by
Michael Crane
parent
c9c7ca2c1d
commit
3962e8926f
Vendored
+56
-52
@@ -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<Keys extends keyof TrustedTypePolicyOptions>(
|
||||
name: string,
|
||||
policyOptions: Pick<TrustedTypePolicyOptions, Keys>,
|
||||
expose?: boolean,
|
||||
): Pick<TrustedTypePolicy, 'name'|Keys>;
|
||||
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<Keys extends keyof TrustedTypePolicyOptions>(
|
||||
name: string,
|
||||
policyOptions: Pick<TrustedTypePolicyOptions, Keys>,
|
||||
expose?: boolean,
|
||||
): Pick<TrustedTypePolicy, 'name'|Keys>;
|
||||
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;
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user