Update trusted types definitions (#38092)

This commit is contained in:
Emanuel Tesař
2019-09-10 06:19:38 +02:00
committed by Mine Starks
parent 8eae05f7f1
commit 562467386a
3 changed files with 43 additions and 21 deletions

View File

@@ -2,6 +2,7 @@
// Project: https://github.com/WICG/trusted-types
// Definitions by: Jakub Vrana <https://github.com/vrana>,
// Damien Engels <https://github.com/engelsdamien>
// Emanuel Tesar <https://github.com/siegrift>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.1
@@ -42,13 +43,30 @@ declare class TrustedTypePolicyFactory {
policyOptions: Pick<TrustedTypePolicyOptions, Keys>,
expose?: boolean,
): Pick<TrustedTypePolicy, 'name'|Keys>;
getExposedPolicy(name: string): TrustedTypePolicy|null;
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 const trustedTypes: TrustedTypePolicyFactory;
declare global {
interface Window {
trustedTypes: TrustedTypePolicyFactory;
TrustedHTML: TrustedHTML;
TrustedScript: TrustedScript;
TrustedScriptURL: TrustedScriptURL;
TrustedURL: TrustedURL;
TrustedTypePolicyOptions: TrustedTypePolicyOptions;
TrustedTypePolicyFactory: TrustedTypePolicyFactory;
}
}
export default trustedTypes;

View File

@@ -1,4 +1,9 @@
const policy = {
import TT from 'trusted-types';
// $ExpectType TrustedTypePolicyFactory
TT;
const rules = {
createHTML: (s: string) => s,
createScript: (s: string) => s,
createScriptURL: (s: string) => s,
@@ -6,42 +11,40 @@ const policy = {
};
// $ExpectType string[]
TrustedTypes.getPolicyNames();
TrustedTypes.createPolicy('default', policy, true);
// $ExpectType TrustedTypePolicy | null
TrustedTypes.getExposedPolicy('default');
window.trustedTypes.getPolicyNames();
window.trustedTypes.createPolicy('default', rules, true);
const trustedTypes = TrustedTypes.createPolicy('test', policy);
const policy = window.trustedTypes.createPolicy('test', rules);
// $ExpectType string
const policyName = trustedTypes.name;
policy.name;
// $ExpectType TrustedHTML
trustedTypes.createHTML('');
policy.createHTML('');
// $ExpectType TrustedScript
trustedTypes.createScript('');
policy.createScript('');
// $ExpectType TrustedScriptURL
trustedTypes.createScriptURL('');
policy.createScriptURL('');
// $ExpectType TrustedURL
trustedTypes.createURL('');
policy.createURL('');
const htmlOnlyPolicy = TrustedTypes.createPolicy('htmlOnly', {
const htmlOnlyPolicy = window.trustedTypes.createPolicy('htmlOnly', {
createHTML: (html: string) => {
return html;
},
});
// $ExpectType string
const htmlOnlyName = htmlOnlyPolicy.name;
htmlOnlyPolicy.name;
// $ExpectType TrustedHTML
const html = htmlOnlyPolicy.createHTML('');
// $ExpectError
const script = htmlOnlyPolicy.createScript('');
htmlOnlyPolicy.createScript('');
// $ExpectType boolean
TrustedTypes.isHTML(html);
window.trustedTypes.isHTML(html);
// $ExpectType boolean
TrustedTypes.isScript(html);
window.trustedTypes.isScript(html);
// $ExpectType boolean
TrustedTypes.isScriptURL(html);
window.trustedTypes.isScriptURL(html);
// $ExpectType boolean
TrustedTypes.isURL(html);
window.trustedTypes.isURL(html);

View File

@@ -2,7 +2,8 @@
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
"es6",
"dom"
],
"noImplicitAny": true,
"noImplicitThis": true,