+1
-1
jsconfig.json
+1
-1
jsconfig.json
+60
test/ts_test.js
+60
test/ts_test.js
···
1
+
// @ts-nocheck
2
+
3
+
// "Test suite" for TypeScript checking in $(), $id() and $tag()
4
+
5
+
function test() {
6
+
7
+
let panel = $(document.querySelector('.panel')); // HTMLElement
8
+
panel.style.display = 'none';
9
+
10
+
/** @type {never} */ let x1 = panel;
11
+
12
+
let link = $(document.querySelector('a.more'), HTMLLinkElement); // HTMLLinkElement
13
+
link.href = 'about:blank';
14
+
15
+
/** @type {never} */ let x2 = link;
16
+
17
+
let html = $(document.parentNode);
18
+
19
+
/** @type {never} */ let x3 = html;
20
+
21
+
document.addEventListener('click', (e) => {
22
+
let target = $(e.target);
23
+
/** @type {never} */ let x4 = target;
24
+
});
25
+
26
+
let text = $(link.innerText);
27
+
28
+
/** @type {never} */ let x5 = text;
29
+
30
+
let login = $id('login'); // HTMLElement
31
+
login.remove();
32
+
33
+
/** @type {never} */ let x6 = login;
34
+
35
+
let loginField = $id('login_field', HTMLInputElement); // HTMLInputElement
36
+
loginField.value = '';
37
+
38
+
/** @type {never} */ let x7 = loginField;
39
+
40
+
let p = $tag('p.details'); // HTMLElement
41
+
p.innerText = 'About';
42
+
43
+
/** @type {never} */ let x8 = p;
44
+
45
+
let p2 = $tag('p.details', { text: 'Info' }); // HTMLElement
46
+
p2.innerText = 'About';
47
+
48
+
/** @type {never} */ let x9 = p2;
49
+
50
+
let img = $tag('img.icon', HTMLImageElement); // HTMLImageElement
51
+
img.loading = 'lazy';
52
+
53
+
/** @type {never} */ let x10 = img;
54
+
55
+
let img2 = $tag('img.icon', { src: accountAPI.user.avatar }, HTMLImageElement); // HTMLImageElement
56
+
img2.loading = 'lazy';
57
+
58
+
/** @type {never} */ let x11 = img2;
59
+
60
+
}
+2
-8
types.d.ts
+2
-8
types.d.ts
···
18
18
type json = Record<string, any>;
19
19
20
20
function $tag(tag: string): HTMLElement;
21
-
function $tag<T>(tag: string, type: new (...args: any[]) => T): T;
21
+
function $tag<T extends HTMLElement>(tag: string, type: new (...args: any[]) => T): T;
22
22
function $tag(tag: string, params: string | object): HTMLElement;
23
-
function $tag<T>(tag: string, params: string | object, type: new (...args: any[]) => T): T;
24
-
25
-
function $id(id: string): HTMLElement;
26
-
function $id<T>(id: string, type: new (...args: any[]) => T): T;
27
-
28
-
function $(element: Node | EventTarget | null): HTMLElement;
29
-
function $<T>(element: Node | EventTarget | null, type: new (...args: any[]) => T): T;
23
+
function $tag<T extends HTMLElement>(tag: string, params: string | object, type: new (...args: any[]) => T): T;
+10
-3
utils.js
+10
-3
utils.js
···
54
54
return /** @type {T} */ (element);
55
55
}
56
56
57
+
/**
58
+
* @template {HTMLElement} T
59
+
* @param {string} name
60
+
* @param {new (...args: any[]) => T} [type]
61
+
* @returns {T}
62
+
*/
63
+
57
64
function $id(name, type) {
58
-
return (document.getElementById(name));
65
+
return /** @type {T} */ (document.getElementById(name));
59
66
}
60
67
61
68
/**
62
-
* @template T
69
+
* @template {HTMLElement} T
63
70
* @param {Node | EventTarget | null} element
64
-
* @param {new (...args: any[]) => T} type
71
+
* @param {new (...args: any[]) => T} [type]
65
72
* @returns {T}
66
73
*/
67
74