forked from Cloud-Player/web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconst.ts
23 lines (16 loc) · 956 Bytes
/
const.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/// <reference types="trusted-types" />
import { TrustedTypesAvailable } from './default';
// tslint:disable-next-line: trusted-types-no-create-policy
const ConstPolicy = TrustedTypesAvailable ? window.trustedTypes.createPolicy('literal-script-url', {
createScriptURL: (s: string) => s
}) : null;
type LiteralString<S extends string> = string extends S ? 'Not a string literal' : S;
export function scriptUrlFromLiteralString<S extends string>(literalString: LiteralString<S>): TrustedScriptURL&string {
console.log('You\'re a good programmer and you are loading script from ', literalString);
if (!TrustedTypesAvailable) {
// For legacy browsers, do nothing. The type system asserts that the function was called with a compile-time
// constant, so this is safe.
return literalString as unknown as TrustedScriptURL&string;
}
return ConstPolicy.createScriptURL(literalString) as TrustedScriptURL&string;
}