build: replace @types/trusted-types dep with minimal type defs (#39211)

The @types/trusted-types type definitions are currently imported in
types.d.ts, which causes them to eventually be imported in core.d.ts.
This forces anyone compiling against @angular/core to provide the
@types/trusted-types package in their compilation unit, which we don't
want.

To address this, get rid of the @types/trusted-types and instead import
a minimal version of the Trusted Types type definitions directly into
Angular's codebase.

Update the existing references to Trusted Types to point to the new
definitions.

PR Close #39211
This commit is contained in:
Bjarki 2020-10-13 20:48:20 +00:00 committed by atscott
parent 497af77d7a
commit 9bfb508b87
6 changed files with 51 additions and 8 deletions

View File

@ -90,7 +90,6 @@
"@types/semver": "^6.0.2",
"@types/shelljs": "^0.8.6",
"@types/systemjs": "0.19.32",
"@types/trusted-types": "^1.0.6",
"@types/yaml": "^1.9.7",
"@types/yargs": "^15.0.5",
"@webcomponents/custom-elements": "^1.1.0",

View File

@ -13,7 +13,6 @@ ts_library(
deps = [
"//packages/zone.js/lib:zone_d_ts",
"@npm//@types/hammerjs",
"@npm//@types/trusted-types",
],
)

View File

@ -0,0 +1,50 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
/**
* @fileoverview
* While Angular only uses Trusted Types internally for the time being,
* references to Trusted Types could leak into our core.d.ts, which would force
* anyone compiling against @angular/core to provide the @types/trusted-types
* package in their compilation unit.
*
* Until https://github.com/microsoft/TypeScript/issues/30024 is resolved, we
* 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.
*
* 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 interface TrustedTypePolicyFactory {
createPolicy(policyName: string, policyOptions: {
createHTML?: (input: string) => string,
createScript?: (input: string) => string,
createScriptURL?: (input: string) => string,
}): TrustedTypePolicy;
getAttributeType(tagName: string, attribute: string): string|null;
}
export interface TrustedTypePolicy {
createHTML(input: string): TrustedHTML;
createScript(input: string): TrustedScript;
createScriptURL(input: string): TrustedScriptURL;
}

View File

@ -17,6 +17,7 @@
*/
import {global} from '../global';
import {TrustedHTML, TrustedScript, TrustedScriptURL, TrustedTypePolicy, TrustedTypePolicyFactory} from './trusted_type_defs';
/**
* The Trusted Types policy, or null if Trusted Types are not

1
packages/types.d.ts vendored
View File

@ -9,7 +9,6 @@
// This file contains all ambient imports needed to compile the packages/ source code
/// <reference types="hammerjs" />
/// <reference types="trusted-types" />
/// <reference lib="es2015" />
/// <reference path="./goog.d.ts" />
/// <reference path="./system.d.ts" />

View File

@ -2358,11 +2358,6 @@
dependencies:
"@types/node" "*"
"@types/trusted-types@^1.0.6":
version "1.0.6"
resolved "https://registry.yarnpkg.com/@types/trusted-types/-/trusted-types-1.0.6.tgz#569b8a08121d3203398290d602d84d73c8dcf5da"
integrity sha512-230RC8sFeHoT6sSUlRO6a8cAnclO06eeiq1QDfiv2FGCLWFvvERWgwIQD4FWqD9A69BN7Lzee4OXwoMVnnsWDw==
"@types/webpack-sources@^0.1.5":
version "0.1.7"
resolved "https://registry.yarnpkg.com/@types/webpack-sources/-/webpack-sources-0.1.7.tgz#0a330a9456113410c74a5d64180af0cbca007141"