Skip to content

Commit cd80895

Browse files
authored
tsc: type all of src/preact/*.js (#37322)
* tsc: typecheck src/preact * rebase fixes * More ryan comments + eslint doin work * opt-error typing * dont even need the AmpElement * rm shadow-embed.ts * update TODO with PR * remove unecessary cast
1 parent c04895b commit cd80895

17 files changed

+347
-250
lines changed

src/base-element.d.ts

+11-29
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
// See src/base-element.js for method documentation.
2+
3+
type Layout_Enum = import('#core/dom/layout').Layout_Enum;
4+
type LayoutSize = import('#core/dom/layout/rect').LayoutSizeDef;
5+
type LayoutRect = import('#core/dom/layout/rect').LayoutRectDef;
6+
27
declare namespace AMP {
38
class BaseElement {
49
static R1(): boolean;
@@ -23,18 +28,18 @@ declare namespace AMP {
2328
getDefaultActionAlias(): string | undefined;
2429
getLayoutPriority(): number;
2530
updateLayoutPriority(pri: number): void;
26-
getLayout(): Layout;
31+
getLayout(): Layout_Enum;
2732
getLayoutBox(): LayoutRect;
2833
getLayoutSize(): LayoutSize;
2934
getWin(): Window;
3035
getAmpDoc(): any;
3136
getVsync(): any;
3237
getConsentPolicy(): string | undefined;
33-
isLayoutSupported(layout: Layout): boolean;
38+
isLayoutSupported(layout: Layout_Enum): boolean;
3439
isAlwaysFixed(): boolean;
3540
upgradeCallback(): null | BaseElement | Promise<BaseElement>;
3641
buildCallback(): void | Promise<void>;
37-
preconnectCallback(onLayout?: boolean);
42+
preconnectCallback(onLayout?: boolean): void;
3843
attachedCallback(): void;
3944
detachedCallback(): void;
4045
setAsContainer(scroller?: Element): void;
@@ -56,14 +61,14 @@ declare namespace AMP {
5661
unlayoutOnPause(): boolean;
5762
reconstructWhenReparented(): boolean;
5863
loadPromise<T>(element: T): Promise<T>;
59-
registerAction(alias: string, handler: any, minTrust: any);
60-
registerDefaultAction(handler: any, alias: string, minTrust: any);
64+
registerAction(alias: string, handler: any, minTrust: any): void;
65+
registerDefaultAction(handler: any, alias: string, minTrust: any): void;
6166
executeAction(invocation: any, deferred?: boolean): any;
6267
forwardEvents(events: string | string[], element: Element): any;
6368
getPlaceholder(): Element;
6469
togglePlaceholder(state: boolean): void;
6570
getFallback(): Element | undefined;
66-
toggleFallback(state: boolean);
71+
toggleFallback(state: boolean): void;
6772
toggleLoading(state: boolean, force?: boolean): void;
6873
getOverflowElement(): Element | undefined;
6974
renderStarted(): void;
@@ -107,29 +112,6 @@ declare namespace AMP {
107112
) => void;
108113
}
109114

110-
declare type Layout =
111-
| 'nodisplay'
112-
| 'fixed'
113-
| 'fixed-height'
114-
| 'responsive'
115-
| 'container'
116-
| 'fill'
117-
| 'flex-item'
118-
| 'fluid'
119-
| 'intrinsic';
120-
121-
declare type LayoutSize = {height: number; width: number};
122-
declare type LayoutRect = {
123-
top: number;
124-
bottom: number;
125-
left: number;
126-
right: number;
127-
width: number;
128-
height: number;
129-
x: number;
130-
y: number;
131-
};
132-
133115
type Mutations = {
134116
[key: string]: null | boolean | string | number | Array<any> | Object;
135117
};

src/core/context/index.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export function unassignSlot(node, slot) {
3737
* Sets (or unsets) the direct parent. If the parent is set, the node will no
3838
* longer try to discover itself.
3939
*
40-
* @param {Node} node
40+
* @param {Node|ShadowRoot} node
4141
* @param {?Node} parent
4242
*/
4343
export function setParent(node, parent) {
@@ -87,7 +87,7 @@ export function rediscoverChildren(node) {
8787
*
8888
* @param {Node} node The target node.
8989
* @param {IContextProp<T, ?>} prop
90-
* @param {function(T):void} setter
90+
* @param {function(*):void} setter
9191
* @param {T} value
9292
* @template T
9393
*/
@@ -101,7 +101,7 @@ export function setProp(node, prop, setter, value) {
101101
*
102102
* @param {Node} node The target node.
103103
* @param {IContextProp<T, ?>} prop
104-
* @param {function(T):void} setter
104+
* @param {function(*):void} setter
105105
* @template T
106106
*/
107107
export function removeProp(node, prop, setter) {

src/core/context/prop.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const EMPTY_DEPS = [];
1616
* type?: Object,
1717
* deps?: IContextProp<DEP, *>[],
1818
* recursive?: boolean | (function(T[]):boolean),
19-
* compute?: function(Node, T[], ...DEP):(T | undefined),
19+
* compute?: function(Node, T[], ...T):(T | undefined),
2020
* defaultValue?: T,
2121
* }} opt_spec
2222
* @return {IContextProp<T, DEP>}

src/core/context/types.d.ts

+14-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {ContextNode} from './node'
1+
import {ContextNode} from './node';
22
export interface IContextProp<T, DEP> {
33
/**
44
* A globally unique key. Extensions must use a fully qualified name such
@@ -7,10 +7,10 @@ export interface IContextProp<T, DEP> {
77
key: string;
88

99
/**
10-
* An optional type object that can be used for a using system. E.g.
10+
* A type object that can be used for a using system. E.g.
1111
* this could be a Preact's Context object.
1212
*/
13-
type?: Object;
13+
type: import('preact').Context<T>;
1414

1515
/**
1616
* An array of dependencies that are required for the `compute` callback.
@@ -44,7 +44,13 @@ export interface IContextProp<T, DEP> {
4444
* 3. If it's a recursive property, the parent value.
4545
* 4. If `deps` are specified - the dep values.
4646
*/
47-
compute: (node: Node, inputs: T[], ...deps: DEP[]) => (T | undefined);
47+
compute(node: Node, inputs: T[], ...deps: T[]): T | undefined;
48+
compute(
49+
node: Node,
50+
inputs: T[],
51+
parentValue: T,
52+
...deps: DEP[]
53+
): T | undefined;
4854

4955
/**
5056
* The default value of a recursive property.
@@ -67,17 +73,17 @@ export type PendingEnumValue = number;
6773

6874
/** The structure for a property's computed values and subscribers. */
6975
export interface IContextPropUsed<T, DEP> {
70-
prop: IContextProp<T, DEP>
76+
prop: IContextProp<T, DEP>;
7177
subscribers: ((value: T) => void)[];
7278
value: T;
7379
pending: PendingEnumValue;
7480
counter: number;
7581
depValues: DEP[];
7682
parentValue: T;
7783
parentContextNode: null | import('./node').ContextNode<?>;
78-
ping: (refreshParent: boolean) => void
79-
pingDep: ((dep: DEP) => void)[]
80-
pingParent: null | ((parentValue: T) => void)
84+
ping: (refreshParent: boolean) => void;
85+
pingDep: ((dep: DEP) => void)[];
86+
pingParent: null | ((parentValue: T) => void);
8187
}
8288
declare global {
8389
interface Node {

src/core/dom/amp-globals.d.ts

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { ReadyState_Enum } from "#core/constants/ready-state";
2+
13
export {};
24

35
declare global {
@@ -19,6 +21,7 @@ declare global {
1921
// expectations rather than the catch-all AMP-specific `AmpElement` class. This
2022
// is already done with the `IPausable` interface.
2123
interface AmpElement extends HTMLElement, IPausable {
24+
readyState: ReadyState_Enum;
2225
sizerElement?: HTMLElement;
2326

2427
getPlaceholder: () => null | Element;

src/core/dom/query.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ function assertIsName(name) {
2626
* This method isn't required for modern builds, can be removed.
2727
* TODO(#37136): This will fail if `root` is a `ShadowRoot`.
2828
*
29-
* @param {HTMLElement|ShadowRoot} root
29+
* @param {Element|ShadowRoot} root
3030
* @param {string} selector
3131
* @return {NodeList}
3232
*/
3333
function scopedQuerySelectionFallback(root, selector) {
34-
const {classList} = /** @type {HTMLElement} */ (root);
34+
const {classList} = /** @type {Element} */ (root);
3535

3636
const unique = 'i-amphtml-scoped';
3737
classList.add(unique);
@@ -44,7 +44,7 @@ function scopedQuerySelectionFallback(root, selector) {
4444
/**
4545
* Finds the first element that matches `selector`, scoped inside `root`.
4646
* Note: in IE, this causes a quick mutation of the element's class list.
47-
* @param {HTMLElement|ShadowRoot} root
47+
* @param {Element|ShadowRoot} root
4848
* @param {string} selector
4949
* @return {?HTMLElement}
5050
*
@@ -263,7 +263,7 @@ export function childNodes(parent, callback) {
263263

264264
/**
265265
* Finds the first child element that has the specified attribute.
266-
* @param {HTMLElement|ShadowRoot} parent
266+
* @param {Element|ShadowRoot} parent
267267
* @param {string} attr
268268
* @return {?Element}
269269
*/

0 commit comments

Comments
 (0)