Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

Add progressive TypeScript compilation (#503)

sarayourfriendc5388da152e8
diff --git a/.github/workflows/pre-build.yaml b/.github/workflows/pre-build.yaml
index fbcd726e5..849cb95ac 100644
--- a/.github/workflows/pre-build.yaml
+++ b/.github/workflows/pre-build.yaml
@@ -46,3 +46,6 @@ jobs:
- name: lint & syntax check
run: npm run lint
+ # run type checker
+ - name: typescript
+ run: npm run types
diff --git a/.husky/pre-commit b/.husky/pre-commit
index 01170b47a..bbb8d485f 100755
--- a/.husky/pre-commit
+++ b/.husky/pre-commit
@@ -12,4 +12,4 @@ if [[ "$CHANGED" == *"$TRANSFILE"* ]] && [[ "$CHANGED" != *"$POTFILE"* ]]; then
fi
# Lint staged files
-npx lint-staged
+npx lint-staged && npm run types
diff --git a/jsconfig.json b/jsconfig.json
deleted file mode 100644
index 7fbbe64c9..000000000
--- a/jsconfig.json
+++ /dev/null
@@ -1,10 +0,0 @@
-{
- "compilerOptions": {
- "baseUrl": "./src",
- "paths": {
- "~/*": ["./*"],
- "~~/*": ["./*"]
- }
- },
- "exclude": ["node_modules", "dist"]
-}
diff --git a/package-lock.json b/package-lock.json
index f3b0d5892..ff769756c 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -74,6 +74,7 @@
"start-server-and-test": "^1.14.0",
"tailwindcss": "^2.2.7",
"tailwindcss-rtl": "^0.7.3",
+ "typescript": "^4.5.2",
"vue-i18n-extract": "^2.0.0",
"vue-jest": "^3.0.7"
},
@@ -32973,11 +32974,10 @@
}
},
"node_modules/typescript": {
- "version": "4.4.4",
- "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.4.4.tgz",
- "integrity": "sha512-DqGhF5IKoBl8WNf8C1gu8q0xZSInh9j1kJJMqT3a94w1JzVaBU4EXOSMrz9yDqMT0xt3selp83fuFMQ0uzv6qA==",
+ "version": "4.5.2",
+ "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.5.2.tgz",
+ "integrity": "sha512-5BlMof9H1yGt0P8/WF+wPNw6GfctgGjXp5hkblpyT+8rkASSmkUKMXrxR0Xg8ThVCi/JnHQiKXeBaEwCeQwMFw==",
"dev": true,
- "peer": true,
"bin": {
"tsc": "bin/tsc",
"tsserver": "bin/tsserver"
@@ -60713,11 +60713,10 @@
}
},
"typescript": {
- "version": "4.4.4",
- "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.4.4.tgz",
- "integrity": "sha512-DqGhF5IKoBl8WNf8C1gu8q0xZSInh9j1kJJMqT3a94w1JzVaBU4EXOSMrz9yDqMT0xt3selp83fuFMQ0uzv6qA==",
- "dev": true,
- "peer": true
+ "version": "4.5.2",
+ "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.5.2.tgz",
+ "integrity": "sha512-5BlMof9H1yGt0P8/WF+wPNw6GfctgGjXp5hkblpyT+8rkASSmkUKMXrxR0Xg8ThVCi/JnHQiKXeBaEwCeQwMFw==",
+ "dev": true
},
"ua-parser-js": {
"version": "0.7.31",
diff --git a/package.json b/package.json
index 4cab68b4f..e7cea36bf 100644
--- a/package.json
+++ b/package.json
@@ -25,6 +25,7 @@
"run-server": "cross-env ENABLE_AUDIO=true nuxt build && npm run start",
"e2e:ci": "start-server-and-test run-server http://localhost:8443 test:e2e",
"generate-e2e-tests": "playwright codegen localhost:8443/",
+ "types": "tsc --noEmit",
"lint": "eslint --ext .js,.vue --ignore-path .gitignore .",
"lintfix": "eslint --fix --ext .js,.vue --ignore-path .gitignore .",
"format": "prettier --write .",
@@ -103,6 +104,7 @@
"start-server-and-test": "^1.14.0",
"tailwindcss": "^2.2.7",
"tailwindcss-rtl": "^0.7.3",
+ "typescript": "^4.5.2",
"vue-i18n-extract": "^2.0.0",
"vue-jest": "^3.0.7"
},
diff --git a/src/composables/use-event-listener-outside.js b/src/composables/use-event-listener-outside.js
index 51fafce9e..31877dbb3 100644
--- a/src/composables/use-event-listener-outside.js
+++ b/src/composables/use-event-listener-outside.js
@@ -32,12 +32,11 @@ export const useEventListenerOutside = ({
const boundEventRef = ref()
watch(
- [containerRef, triggerRef, shouldListenRef],
- /**
- * @param {[HTMLElement, HTMLElement, boolean]} deps
- * @param {unknown} _
- * @param {(cb: () => void) => void} onInvalidate
- */
+ /** @type {const} */ ([
+ containerRef,
+ triggerRef,
+ shouldListenRef || ref(false),
+ ]),
([container, trigger, shouldListen], _, onInvalidate) => {
if (boundEventRef.value && !shouldListen) {
const document = getDocument(container)
@@ -50,9 +49,9 @@ export const useEventListenerOutside = ({
* @param {Event} event
*/
const onEvent = (event) => {
- if (!listener) return
+ if (!listener || !container || !(event.target instanceof Element))
+ return
const target = event.target
- if (!container) return
// When an element is unmounted right after it receives focus, the focus
// event is triggered after that, when the element isn't part of the
diff --git a/tsconfig.json b/tsconfig.json
new file mode 100644
index 000000000..c3aa6a696
--- /dev/null
+++ b/tsconfig.json
@@ -0,0 +1,40 @@
+{
+ "compilerOptions": {
+ "allowJs": true,
+ "checkJs": true,
+ "allowSyntheticDefaultImports": true,
+ "jsx": "preserve",
+ "target": "esnext",
+ "module": "esnext",
+ "lib": ["dom", "esnext"],
+ "declaration": true,
+ "declarationMap": true,
+ "noEmit": true,
+ "isolatedModules": true,
+ "skipLibCheck": true,
+
+ /* Strict Type-Checking Options */
+ "strict": true,
+
+ /* Additional Checks */
+ "noUnusedLocals": true,
+ "noUnusedParameters": true,
+ "noImplicitReturns": true,
+ "noFallthroughCasesInSwitch": true,
+ "importsNotUsedAsValues": "error",
+
+ /* Module Resolution Options */
+ "moduleResolution": "node",
+ "resolveJsonModule": true,
+ "typeRoots": ["./node_modules/@types"],
+ "paths": {
+ "~/*": ["./src/*"],
+ "~~/*": ["./*"]
+ },
+ "baseUrl": "./"
+ },
+ "include": [
+ "src/composables/types.js",
+ "src/composables/use-event-listener-outside.js"
+ ]
+}