fix(core): mark Trusted Types as declarations (#39471)

Angular-internal type definitions for Trusted Types were added in #39211.
When compiled using the Closure compiler with certain optimization
flags, identifiers from these type definitions (such as createPolicy)
are currently uglified and renamed to shorter strings. This causes
Angular applications compiled in this way to fail to create a Trusted
Types policy, and fall bock to using strings.

To fix this, mark the internal Trusted Types definitions as declarations
using the "declare" keyword. Also convert types to interfaces, for
the reasons explained in https://ncjamieson.com/prefer-interfaces/

PR Close #39471
This commit is contained in:
Bjarki 2020-10-28 14:23:55 +00:00 committed by Joey Perrott
parent 8af32fcb16
commit f54662e931
2 changed files with 20 additions and 18 deletions

View File

@ -28,24 +28,25 @@ import {global} from '../util';
* will keep Angular's public API surface free of references to Trusted Types.
* For internal and semi-private APIs that need to reference Trusted Types, the
* minimal type definitions for the Trusted Types API provided by this module
* should be used instead.
* should be used instead. They are marked as "declare" to prevent them from
* being renamed by compiler optimization.
*
* Adapted from
* https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/trusted-types/index.d.ts
* but restricted to the API surface used within Angular.
*/
export type TrustedScript = {
__brand__: 'TrustedScript'
};
export declare interface TrustedScript {
__brand__: 'TrustedScript';
}
export interface TrustedTypePolicyFactory {
export declare interface TrustedTypePolicyFactory {
createPolicy(policyName: string, policyOptions: {
createScript?: (input: string) => string,
}): TrustedTypePolicy;
}
export interface TrustedTypePolicy {
export declare interface TrustedTypePolicy {
createScript(input: string): TrustedScript;
}

View File

@ -17,24 +17,25 @@
* will keep Angular's public API surface free of references to Trusted Types.
* For internal and semi-private APIs that need to reference Trusted Types, the
* minimal type definitions for the Trusted Types API provided by this module
* should be used instead.
* should be used instead. They are marked as "declare" to prevent them from
* being renamed by compiler optimization.
*
* Adapted from
* https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/trusted-types/index.d.ts
* but restricted to the API surface used within Angular.
*/
export type TrustedHTML = {
__brand__: 'TrustedHTML'
};
export type TrustedScript = {
__brand__: 'TrustedScript'
};
export type TrustedScriptURL = {
__brand__: 'TrustedScriptURL'
};
export declare interface TrustedHTML {
__brand__: 'TrustedHTML';
}
export declare interface TrustedScript {
__brand__: 'TrustedScript';
}
export declare interface TrustedScriptURL {
__brand__: 'TrustedScriptURL';
}
export interface TrustedTypePolicyFactory {
export declare interface TrustedTypePolicyFactory {
createPolicy(policyName: string, policyOptions: {
createHTML?: (input: string) => string,
createScript?: (input: string) => string,
@ -43,7 +44,7 @@ export interface TrustedTypePolicyFactory {
getAttributeType(tagName: string, attribute: string): string|null;
}
export interface TrustedTypePolicy {
export declare interface TrustedTypePolicy {
createHTML(input: string): TrustedHTML;
createScript(input: string): TrustedScript;
createScriptURL(input: string): TrustedScriptURL;