Skip to content

Commit 77264d2

Browse files
Migrate top level JS files to TS.
1 parent ea63aa4 commit 77264d2

49 files changed

Lines changed: 206 additions & 53 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { omit } from 'lodash';
2727
import { createTestRegistry } from '../../tests/js/utils';
2828

2929
const registry = createTestRegistry();
30+
// @ts-expect-error The WPDataRegistry type is missing the stores property.
3031
const firstPartyStores = omit( registry.stores, 'core/data' );
3132

3233
describe( 'all data stores', () => {
@@ -36,6 +37,7 @@ describe( 'all data stores', () => {
3637
const listener = jest.fn();
3738
registry.subscribe( listener );
3839

40+
// @ts-expect-error The WPDataRegistry type is missing the stores property.
3941
registry.stores[ storeName ].store.dispatch( {
4042
type: '@@NON_EXISTENT_ACTION_TYPE@@',
4143
} );
Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,14 @@ import setUpAdvancedTracking from './analytics-advanced-tracking/set-up-advanced
2828
*
2929
* @since 1.18.0
3030
*
31-
* @param {string} action Event action / event name.
32-
* @param {Object} metadata Additional event metadata to send, or `null`.
31+
* @param {string} action Event action / event name.
32+
* @param {Record< string, unknown >|null} metadata Additional event metadata to send, or `null`.
33+
* @return {void}
3334
*/
34-
function sendEvent( action, metadata ) {
35+
function sendEvent(
36+
action: string,
37+
metadata: Record< string, unknown > | null
38+
) {
3539
window.gtag( 'event', action, metadata || undefined );
3640
}
3741

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Site Kit by Google, Copyright 2026 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
export type AdvancedTrackingEvent = {
18+
action: string;
19+
on: string;
20+
selector: string;
21+
metadata?: Record< string, unknown >;
22+
};
Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
/**
2020
* WordPress dependencies
2121
*/
22-
import apiFetch from '@wordpress/api-fetch__non-shim';
22+
import apiFetch, {
23+
type APIFetchMiddleware,
24+
} from '@wordpress/api-fetch__non-shim';
2325

2426
/**
2527
* Internal dependencies
@@ -29,6 +31,14 @@ import createDedupeMiddleware, {
2931
logDuplicate,
3032
} from '@/js/googlesitekit/api/middleware/deduplication';
3133

34+
declare module '@wordpress/api-fetch' {
35+
interface ApiFetch {
36+
rootURLMiddleware: APIFetchMiddleware;
37+
dedupeMiddleware: APIFetchMiddleware;
38+
preloadingMiddleware: APIFetchMiddleware;
39+
}
40+
}
41+
3242
const { nonce, nonceEndpoint, preloadedData, rootURL } =
3343
global._googlesitekitAPIFetchData || {};
3444

@@ -38,7 +48,9 @@ apiFetch.rootURLMiddleware = apiFetch.createRootURLMiddleware( rootURL );
3848
apiFetch.dedupeMiddleware = createDedupeMiddleware( {
3949
onDuplicate: logDuplicate,
4050
} );
41-
apiFetch.preloadingMiddleware = createPreloadingMiddleware( preloadedData );
51+
apiFetch.preloadingMiddleware = createPreloadingMiddleware(
52+
preloadedData
53+
) as APIFetchMiddleware; // @TODO Remove this when `createPreloadingMiddleware` is properly typed.
4254

4355
apiFetch.use( apiFetch.nonceMiddleware );
4456
apiFetch.use( apiFetch.mediaUploadMiddleware );
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ domReady( () => {
3838

3939
if ( renderTarget ) {
4040
render(
41+
// @ts-expect-error Root is not properly typed yet.
4142
<Root viewContext={ VIEW_CONTEXT_ACTIVATION }>
4243
<ActivationApp />
4344
</Root>,

assets/js/googlesitekit-ad-blocking-recovery.js renamed to assets/js/googlesitekit-ad-blocking-recovery.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ domReady( () => {
3535

3636
if ( renderTarget ) {
3737
render(
38+
// @ts-expect-error Root is not properly typed yet.
3839
<Root viewContext={ VIEW_CONTEXT_AD_BLOCKING_RECOVERY }>
3940
<AdBlockingRecoveryApp />
4041
</Root>,

assets/js/googlesitekit-admin-pointers-tracking.js renamed to assets/js/googlesitekit-admin-pointers-tracking.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@
1919
/**
2020
* Internal dependencies.
2121
*/
22+
import { GATrackingEventArgs } from './types/GATrackingEventArgs';
2223
import { trackEvent } from './util';
2324

2425
const TRACKING_KEYS = [ 'view', 'click', 'dismiss' ];
2526

26-
function fireTrackingEvent( eventConfig ) {
27+
function fireTrackingEvent( eventConfig: GATrackingEventArgs ) {
2728
if ( ! eventConfig || ! eventConfig.category || ! eventConfig.action ) {
2829
return null;
2930
}
@@ -36,17 +37,20 @@ function fireTrackingEvent( eventConfig ) {
3637
return trackEvent( category, action );
3738
}
3839

39-
function registerPointerTracking( slug, tracking ) {
40+
function registerPointerTracking(
41+
slug: string,
42+
tracking: Record< string, GATrackingEventArgs >
43+
) {
4044
if ( ! tracking || ! Object.keys( tracking ).length ) {
4145
return { onDismiss: null };
4246
}
4347

4448
const fired = TRACKING_KEYS.reduce(
4549
( acc, key ) => ( { ...acc, [ key ]: false } ),
46-
{}
50+
{} as Record< string, boolean >
4751
);
4852

49-
function fireOnce( key ) {
53+
function fireOnce( key: string ) {
5054
if ( fired[ key ] || ! tracking[ key ] ) {
5155
return null;
5256
}
@@ -64,7 +68,7 @@ function registerPointerTracking( slug, tracking ) {
6468
document.documentElement.ownerDocument;
6569
const ctaSelector = `.${ slug } .googlesitekit-pointer-cta`;
6670

67-
function handleClick( event ) {
71+
function handleClick( event: Event ) {
6872
const target = event.target instanceof Element ? event.target : null;
6973
if ( ! target || ! target.closest( ctaSelector ) ) {
7074
return;
@@ -82,7 +86,7 @@ function registerPointerTracking( slug, tracking ) {
8286

8387
if ( shouldDeferNavigation ) {
8488
track.finally( () => {
85-
ownerDocument.defaultView.location.assign( href );
89+
ownerDocument.defaultView?.location.assign( href );
8690
} );
8791
}
8892
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ const init = once( () => {
5252
: VIEW_CONTEXT_ADMIN_BAR;
5353

5454
render(
55+
// @ts-expect-error Root is not properly typed yet.
5556
<Root viewContext={ viewContext }>
5657
<AdminBarApp />
5758
</Root>,

0 commit comments

Comments
 (0)