Skip to content

Commit

Permalink
Fix declarations to avoid globalThis as any. (#2043)
Browse files Browse the repository at this point in the history
* Remove unused type declarations.

* Revert "Cast `window` to a new type (instead of extending it) to prevent collisions. (#1904)"

This reverts commit ec5ab1b, with resolved conflicts.

* Remove unused, duplicate declarations for types added by other packages in the repo.

* Remove casts to `any` for `litHtmlPlatformSupport`.

* Remove casts to `any` for `reactiveElementPlatformSupport`.

* Remove casts to `any` for `litElementPlatformSupport`.

* Remove casts to `any` for `litHtmlVersions`, `reactiveElementVersions`, and `litElementVersions`.

* Remove casts to `any` for `litElementHydrateSupport`.

* Remove last `any` cast for `reactiveElementPlatformSupport` in tests.

* Add a changeset.
  • Loading branch information
bicknellr committed Aug 25, 2021
1 parent 0312f3e commit 761375a
Show file tree
Hide file tree
Showing 12 changed files with 44 additions and 37 deletions.
7 changes: 7 additions & 0 deletions .changeset/unlucky-apes-brush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'lit-element': patch
'lit-html': patch
'@lit/reactive-element': patch
---

Update some internal types to avoid casting `globalThis` to `any` to retrieve globals where possible.
14 changes: 11 additions & 3 deletions packages/lit-element/src/env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@
* SPDX-License-Identifier: BSD-3-Clause
*/

interface Window {
// eslint-disable-next-line no-var
declare var litElementHydrateSupport:
| undefined
// eslint-disable-next-line @typescript-eslint/no-explicit-any
litElementPlatformSupport: (options: {[index: string]: any}) => void;
}
| ((options: {LitElement: any}) => void);
// eslint-disable-next-line no-var
declare var litElementPlatformSupport:
| undefined
// eslint-disable-next-line @typescript-eslint/no-explicit-any
| ((options: {LitElement: any}) => void);
// eslint-disable-next-line no-var
declare var litElementVersions: undefined | Array<string>;
2 changes: 1 addition & 1 deletion packages/lit-element/src/experimental-hydrate-support.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ interface PatchableLitElement extends HTMLElement {
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
(globalThis as any)['litElementHydrateSupport'] = ({
globalThis.litElementHydrateSupport = ({
LitElement,
}: {
LitElement: PatchableLitElement;
Expand Down
9 changes: 3 additions & 6 deletions packages/lit-element/src/lit-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,10 @@ export class LitElement extends ReactiveElement {
}

// Install hydration if available
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(globalThis as any)['litElementHydrateSupport']?.({LitElement});
globalThis.litElementHydrateSupport?.({LitElement});

// Apply polyfills if available
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(globalThis as any)['litElementPlatformSupport']?.({LitElement});
globalThis.litElementPlatformSupport?.({LitElement});

// DEV mode warnings
if (DEV_MODE) {
Expand Down Expand Up @@ -225,5 +223,4 @@ export const _$LE = {
// IMPORTANT: do not change the property name or the assignment expression.
// This line will be used in regexes to search for LitElement usage.
// TODO(justinfagnani): inject version number at build time
// eslint-disable-next-line @typescript-eslint/no-explicit-any
((globalThis as any)['litElementVersions'] ??= []).push('3.0.0-rc.3');
(globalThis.litElementVersions ??= []).push('3.0.0-rc.3');
3 changes: 1 addition & 2 deletions packages/lit-element/src/polyfill-support.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ interface PatchableLitElement extends HTMLElement {
renderOptions: RenderOptions;
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
(globalThis as any)['litElementPlatformSupport'] ??= ({
globalThis.litElementPlatformSupport ??= ({
LitElement,
}: {
LitElement: PatchableLitElement;
Expand Down
9 changes: 6 additions & 3 deletions packages/lit-html/src/env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
* SPDX-License-Identifier: BSD-3-Clause
*/

interface Window {
// eslint-disable-next-line no-var
declare var litHtmlPlatformSupport:
| undefined
// eslint-disable-next-line @typescript-eslint/no-explicit-any
litHtmlPlatformSupport: (template: unknown, childPart: unknown) => void;
}
| (((template: any, childPart: any) => void) & {noPatchSupported?: boolean});
// eslint-disable-next-line no-var
declare var litHtmlVersions: undefined | Array<string>;
6 changes: 2 additions & 4 deletions packages/lit-html/src/lit-html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1629,11 +1629,9 @@ export const _$LH = {
};

// Apply polyfills if available
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(globalThis as any)['litHtmlPlatformSupport']?.(Template, ChildPart);
globalThis.litHtmlPlatformSupport?.(Template, ChildPart);

// IMPORTANT: do not change the property name or the assignment expression.
// This line will be used in regexes to search for lit-html usage.
// TODO(justinfagnani): inject version number at build time
// eslint-disable-next-line @typescript-eslint/no-explicit-any
((globalThis as any)['litHtmlVersions'] ??= []).push('2.0.0-rc.4');
(globalThis.litHtmlVersions ??= []).push('2.0.0-rc.4');
7 changes: 2 additions & 5 deletions packages/lit-html/src/polyfill-support.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,7 @@ const ENABLE_SHADYDOM_NOPATCH = true;
* * ChildPart.prototype._$getTemplate
* * ChildPart.prototype._$setValue
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(globalThis as any)['litHtmlPlatformSupport'] ??= (
globalThis.litHtmlPlatformSupport ??= (
Template: PatchableTemplateConstructor,
ChildPart: PatchableChildPartConstructor
) => {
Expand Down Expand Up @@ -285,7 +284,5 @@ const ENABLE_SHADYDOM_NOPATCH = true;
};

if (ENABLE_SHADYDOM_NOPATCH) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-misused-new
(globalThis as any)['litHtmlPlatformSupport'].noPatchSupported =
ENABLE_SHADYDOM_NOPATCH;
globalThis.litHtmlPlatformSupport!.noPatchSupported = ENABLE_SHADYDOM_NOPATCH;
}
9 changes: 6 additions & 3 deletions packages/reactive-element/src/env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
* SPDX-License-Identifier: BSD-3-Clause
*/

interface Window {
// eslint-disable-next-line no-var
declare var reactiveElementPlatformSupport:
| undefined
// eslint-disable-next-line @typescript-eslint/no-explicit-any
reactiveElementPlatformSupport: (options: {[index: string]: any}) => void;
}
| ((options: {ReactiveElement: any}) => void);
// eslint-disable-next-line no-var
declare var reactiveElementVersions: undefined | Array<string>;

// Augment existing types with styling API
interface ShadowRoot {
Expand Down
3 changes: 1 addition & 2 deletions packages/reactive-element/src/polyfill-support.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ interface PatchableReactiveElement extends HTMLElement {
renderOptions: RenderOptions;
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
(globalThis as any)['reactiveElementPlatformSupport'] ??= ({
globalThis.reactiveElementPlatformSupport ??= ({
ReactiveElement,
}: {
ReactiveElement: PatchableReactiveElement;
Expand Down
9 changes: 3 additions & 6 deletions packages/reactive-element/src/reactive-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ if (DEV_MODE) {
// Issue platform support warning.
if (
window.ShadyDOM?.inUse &&
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(globalThis as any)['reactiveElementPlatformSupport'] === undefined
globalThis.reactiveElementPlatformSupport === undefined
) {
console.warn(
`Shadow DOM is being polyfilled via ShadyDOM but ` +
Expand Down Expand Up @@ -1242,8 +1241,7 @@ export abstract class ReactiveElement
}

// Apply polyfills if available
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(globalThis as any)['reactiveElementPlatformSupport']?.({ReactiveElement});
globalThis.reactiveElementPlatformSupport?.({ReactiveElement});

// Dev mode warnings...
if (DEV_MODE) {
Expand Down Expand Up @@ -1286,5 +1284,4 @@ declare global {
// IMPORTANT: do not change the property name or the assignment expression.
// This line will be used in regexes to search for ReactiveElement usage.
// TODO(justinfagnani): inject version number at build time
// eslint-disable-next-line @typescript-eslint/no-explicit-any
((globalThis as any)['reactiveElementVersions'] ??= []).push('1.0.0-rc.3');
(globalThis.reactiveElementVersions ??= []).push('1.0.0-rc.3');
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ if (DEV_MODE) {
let warnings: string[] = [];

const missingPlatformSupport =
window.ShadyDOM?.inUse &&
!(globalThis as any)['reactiveElementPlatformSupport'];
window.ShadyDOM?.inUse && !globalThis.reactiveElementPlatformSupport;

const consoleWarn = console.warn;

Expand Down

0 comments on commit 761375a

Please sign in to comment.