Skip to content

Commit ca52f40

Browse files
authored
Marginally improve type checkability of code (#2337)
#2336 is an example of a bug that type checking would've stopped. To work towards adding type checking warnings, this PR adds a globals.d.ts defining some APIs that are missing from regular TypeScript and adjusts a few extensions to remove a few warnings. If you read the extension changes, you should be able to convince yourself that this change has no affect on extension behavior and doesn't need any testing
1 parent 7a5dd2a commit ca52f40

File tree

8 files changed

+105
-9
lines changed

8 files changed

+105
-9
lines changed

extensions/0832/rxFS.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,6 @@
216216

217217
search({ STR }) {
218218
Search = "";
219-
i = 0;
220219
str = btoa(unescape(encodeURIComponent(STR)));
221220
for (var i in rxFSsy) {
222221
if (!(rxFSsy[i].indexOf(str) == undefined)) {
@@ -228,7 +227,6 @@
228227

229228
list({ STR }) {
230229
Search = "";
231-
i = 0;
232230
str = btoa(unescape(encodeURIComponent(STR)));
233231
for (var i in rxFSsy) {
234232
if (rxFSsy[i].slice(0, str.length) == str) {

extensions/0832/rxFS2.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,6 @@
241241

242242
search({ STR }) {
243243
Search = "";
244-
i = 0;
245244
str = encodeURIComponent(STR);
246245
for (var i in rxFSsy) {
247246
if (!(rxFSsy[i].indexOf(str) == undefined)) {
@@ -253,7 +252,6 @@
253252

254253
list({ STR }) {
255254
Search = "";
256-
i = 0;
257255
str = encodeURIComponent(STR);
258256
for (var i in rxFSsy) {
259257
if (rxFSsy[i].slice(0, str.length) == str) {

extensions/CubesterYT/WindowControls.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,6 @@
559559
default:
560560
"Are you sure you want to close this window?\n\n(This message will not appear when the project is packaged)",
561561
});
562-
// @ts-expect-error
563562
if (typeof ScratchBlocks === "undefined" || confirm(editorConfirmation)) {
564563
window.close();
565564
}

extensions/DT/cameracontrols.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
// tell resize to update camera as well
7575
vm.runtime.on("STAGE_SIZE_CHANGED", (_) => updateCamera());
7676

77-
vm.runtime.on("RUNTIME_DISPOSED", (_) => {
77+
vm.runtime.on("RUNTIME_DISPOSED", () => {
7878
cameraX = 0;
7979
cameraY = 0;
8080
cameraZoom = 100;

extensions/Lily/ClonesPlus.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,6 @@
708708
}
709709

710710
getVariables() {
711-
// @ts-expect-error - Blockly not typed yet
712711
const variables =
713712
typeof Blockly === "undefined"
714713
? []

extensions/TheShovel/ShovelUtils.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,6 @@
238238
if (!target || target.isStage) {
239239
return;
240240
}
241-
// @ts-expect-error
242241
if (typeof ScratchBlocks !== "undefined") {
243242
if (
244243
!confirm(
@@ -272,7 +271,6 @@
272271
}
273272

274273
importProject({ TEXT }) {
275-
// @ts-ignore
276274
if (typeof ScratchBlocks !== "undefined") {
277275
// We are in the editor. Ask before loading a new project to avoid unrecoverable data loss.
278276
if (

globals.d.ts

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/**
2+
* @fileoverview
3+
* Defines extra APIs that exist in real web browsers but that are missing from TypeScript's default types.
4+
*/
5+
6+
interface Element {
7+
/**
8+
* requestFullscreen() but available in Safari.
9+
*/
10+
webkitRequestFullscreen?(): unknown;
11+
}
12+
13+
interface Document {
14+
/**
15+
* exitFullscreen() but available in Safari.
16+
*/
17+
webkitExitFullscreen?(): unknown;
18+
/**
19+
* fullscreenElement but available in Safari.
20+
*/
21+
webkitFullscreenElement?: Element;
22+
}
23+
24+
/**
25+
* https://developer.mozilla.org/en-US/docs/Web/API/NDEFReader
26+
*/
27+
declare class NDEFReader extends EventTarget {
28+
constructor();
29+
scan(options?: { signal?: AbortSignal }): Promise<void>;
30+
onreading?(event: Event & { message: NDEFMessage }): void;
31+
onreadingerror?(event: Event): void;
32+
}
33+
34+
type TypedArray =
35+
| Uint8Array
36+
| Int8Array
37+
| Uint16Array
38+
| Int16Array
39+
| Uint32Array
40+
| Int32Array
41+
| BigUint64Array
42+
| BigInt64Array;
43+
44+
/**
45+
* https://developer.mozilla.org/en-US/docs/Web/API/NDEFMessage/NDEFMessage
46+
*/
47+
declare class NDEFMessage {
48+
constructor(
49+
records: Array<{
50+
data?: string | ArrayBuffer | TypedArray | DataView | NDEFRecord[];
51+
encoding?: string;
52+
id?: string;
53+
lang?: string;
54+
mediaType?: string;
55+
recordType?: string;
56+
}>
57+
);
58+
readonly records: NDEFRecord[];
59+
}
60+
61+
/**
62+
* https://developer.mozilla.org/en-US/docs/Web/API/NDEFRecord
63+
*/
64+
declare class NDEFRecord {
65+
constructor();
66+
readonly recordType: string;
67+
readonly mediaType: string;
68+
readonly id: string;
69+
readonly data: DataView;
70+
readonly encoding: string | null;
71+
readonly lang: string | null;
72+
toRecords(): NDEFRecord[];
73+
}
74+
75+
interface NetworkInformation {
76+
downlink: number;
77+
downlinkMax: number;
78+
effectiveType: string;
79+
rtt: number;
80+
saveData: boolean;
81+
type:
82+
| "bluetooth"
83+
| "cellular"
84+
| "ethernet"
85+
| "none"
86+
| "wifi"
87+
| "wimax"
88+
| "other"
89+
| "unknown";
90+
}
91+
92+
interface Navigator {
93+
connection?: NetworkInformation;
94+
}
95+
96+
declare namespace Scratch.extensions {
97+
/**
98+
* Most extensions fail the type checking of @turbowarp/types-tw's default register().
99+
* That error generally isn't very useful - so for now we'll just add this overload so
100+
* that those errors don't appear.
101+
*/
102+
function register(extensionObj: any): void;
103+
}

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"include": [
2020
"node_modules/@turbowarp/types/types/scratch-vm-extension.d.ts",
2121
"node_modules/@turbowarp/types/types/scratchx-extension.d.ts",
22+
"globals.d.ts",
2223
"extensions/**/*",
2324
"website/**/*"
2425
]

0 commit comments

Comments
 (0)