DefinitelyTyped/types/trusted-types/trusted-types-tests.ts
Damien Engels 551b328862 Update trusted-types definitions to allow for partial policies. (#34239)
Also adds:
 - TrustedTypePolicy#name property
 - TrusteTypePolicyFactory#is(HTML|Script|ScriptURL|URL) functions

Updates the tests to assert types properly as well as add coverage for
all the new additions.

No version update is needed as the api didn't change. This is just a
better description of the actual behavior.
2019-03-27 16:58:18 -07:00

48 lines
1.2 KiB
TypeScript

const policy = {
createHTML: (s: string) => s,
createScript: (s: string) => s,
createScriptURL: (s: string) => s,
createURL: (s: string) => s,
};
// $ExpectType string[]
TrustedTypes.getPolicyNames();
TrustedTypes.createPolicy('default', policy, true);
// $ExpectType TrustedTypePolicy | null
TrustedTypes.getExposedPolicy('default');
const trustedTypes = TrustedTypes.createPolicy('test', policy);
// $ExpectType string
const policyName = trustedTypes.name;
// $ExpectType TrustedHTML
trustedTypes.createHTML('');
// $ExpectType TrustedScript
trustedTypes.createScript('');
// $ExpectType TrustedScriptURL
trustedTypes.createScriptURL('');
// $ExpectType TrustedURL
trustedTypes.createURL('');
const htmlOnlyPolicy = TrustedTypes.createPolicy('htmlOnly', {
createHTML: (html: string) => {
return html;
},
});
// $ExpectType string
const htmlOnlyName = htmlOnlyPolicy.name;
// $ExpectType TrustedHTML
const html = htmlOnlyPolicy.createHTML('');
// $ExpectError
const script = htmlOnlyPolicy.createScript('');
// $ExpectType boolean
TrustedTypes.isHTML(html);
// $ExpectType boolean
TrustedTypes.isScript(html);
// $ExpectType boolean
TrustedTypes.isScriptURL(html);
// $ExpectType boolean
TrustedTypes.isURL(html);