Skip to content

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

Add support for TypeScript in Vue SFCs. (#1163)

sarayourfriend51f75fe62990
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index 9eeb2b2d3..2e2bfc20e 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -44,9 +44,17 @@ You can also configure your editor of choice with a ESLint plugin so you can get
### Types
-We use JSDoc to document type definitions for functions throughout the code base with the eventual goal of using the TypeScript compiler to actually run a type-checker against our code. To facilitate this, we use the TypeScript "flavor" for JSDoc. Please familiarize yourself with the [documentation for TypeScript in JSDoc here](https://www.typescriptlang.org/docs/handbook/jsdoc-supported-types.html).
+We use native TypeScript everywhere we can. Currently there are very few edge cases where it is not possible to use TypeScript, primarily in older components that do not use the composition-api or that rely on Nuxt's auto-component importing.
-TypeScript support in Vue 2 (which we're currently locked into due to Nuxt's current limitations), is an afterthought. Therefore, types are primarily focused on the Vuex store and API service/utility functions. However, it is possible to add type annotations to some parts of Vue components, like the return value of the `data` function. Whenever possible, contributors should add or correct types to functions they are adding or modifying.
+TypeScript support for Vue 2 is limited to composition-api based components (i.e., components that use `defineComponent` and declare a `setup` function). If you'd like to use TypeScript for a Vue SFC, add `lang="ts"` to the `script` tag and add the file to the `tsconfig.json` `includes` array.
+
+Elsewhere, simply use (and prefer) the `.ts` extension for all new files.
+
+See [Vue's official documentation about IDE support for Vue with TypeScript](https://vuejs.org/guide/typescript/overview.html#ide-support) for guides on how to set up your editor.
+
+#### JSDoc TypeScript
+
+Some older modules use JSDoc flavored TypeScript. This was primarily used before the project had included support for native TypeScript. Please do not extend the use of JSDoc TypeScript outside of modules that already use it. If you feel so inclined, we always welcome PRs updating such modules to native TypeScript.
### Styles/CSS
diff --git a/package.json b/package.json
index dc770266d..66e225cf5 100644
--- a/package.json
+++ b/package.json
@@ -34,7 +34,7 @@
"update-tapes": "cross-env UPDATE_TAPES=true pnpm test:e2e",
"fix-tape-permissions": "docker run -it --rm --mount type=bind,source=$(pwd),target=/app node:alpine chmod -R 0777 /app/test/tapes",
"generate-playwright-test": "playwright codegen localhost:8443/",
- "types": "tsc --noEmit",
+ "types": "vue-tsc --noEmit",
"lint": "eslint --ext .js,.vue,.ts --ignore-path .gitignore --ignore-path .eslintignore .",
"lintfix": "pnpm run lint -- --fix",
"format": "prettier --write --loglevel=warn .",
@@ -107,6 +107,7 @@
"@types/uuid": "^8.3.4",
"@typescript-eslint/eslint-plugin": "^5.15.0",
"@typescript-eslint/parser": "^5.15.0",
+ "@vue/runtime-dom": "^3.2.31",
"@vue/test-utils": "^1.1.3",
"autoprefixer": "^10.4.0",
"babel-jest": "^26.6.3",
@@ -138,7 +139,8 @@
"ts-node": "^10.5.0",
"typescript": "^4.6.2",
"vue-i18n-extract": "^2.0.0",
- "vue-jest": "^3.0.7"
+ "vue-jest": "^3.0.7",
+ "vue-tsc": "^0.33.6"
},
"engines": {
"node": ">= 16.0.0",
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 43b39fc41..b9d0a4a0c 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -31,6 +31,7 @@ specifiers:
'@types/uuid': ^8.3.4
'@typescript-eslint/eslint-plugin': ^5.15.0
'@typescript-eslint/parser': ^5.15.0
+ '@vue/runtime-dom': ^3.2.31
'@vue/test-utils': ^1.1.3
'@wordpress/is-shallow-equal': ^4.3.1
autoprefixer: ^10.4.0
@@ -90,6 +91,7 @@ specifiers:
vue-i18n: ^8.26.5
vue-i18n-extract: ^2.0.0
vue-jest: ^3.0.7
+ vue-tsc: ^0.33.6
dependencies:
'@babel/eslint-parser': [email protected]
@@ -152,6 +154,7 @@ devDependencies:
'@types/uuid': 8.3.4
'@typescript-eslint/eslint-plugin': 5.15.0_5400bdff049a1444f15d03ace6195cd2
'@typescript-eslint/parser': [email protected][email protected]
+ '@vue/runtime-dom': 3.2.31
'@vue/test-utils': 1.3.0
autoprefixer: [email protected]
babel-jest: 26.6.3
@@ -183,6 +186,7 @@ devDependencies:
typescript: 4.6.2
vue-i18n-extract: 2.0.4
packages:
@@ -5826,6 +5830,65 @@ packages:
eslint-visitor-keys: 3.3.0
dev: true
+ /@volar/code-gen/0.33.6:
+ resolution: {integrity: sha512-Ac2C8q530x2f6OrebHJKoSSaQPM3Gc21pahsJ2X3Nnuj01rTT1cOh/M0nUwJ9nwOWojU4DDVkZMLtKBz93QWrQ==}
+ dependencies:
+ '@volar/source-map': 0.33.6
+ dev: true
+
+ /@volar/pug-language-service/0.33.6:
+ resolution: {integrity: sha512-uKO6rtO/U9J7yWjp0u74WdPYN8YjoRjENWi/fgII/vH65XB+zzoUiBkUnLqzdYNGPrKbxTPUaw0KPEYAUHhN8A==}
+ dependencies:
+ '@volar/code-gen': 0.33.6
+ '@volar/shared': 0.33.6
+ '@volar/source-map': 0.33.6
+ '@volar/transforms': 0.33.6
+ pug-lexer: 5.0.1
+ pug-parser: 6.0.0
+ vscode-languageserver-textdocument: 1.0.4
+ vscode-languageserver-types: 3.17.0-next.9
+ dev: true
+
+ /@volar/shared/0.33.6:
+ resolution: {integrity: sha512-JJRcPolX5VjqJ7bWBlsLhqeXSDhHQIAqa/cKfrgpKeqlhdAR7h7B/j4RZmDQefHZWydIxRqtpE8oMkb42gnJyA==}
+ dependencies:
+ upath: 2.0.1
+ vscode-jsonrpc: 8.0.0-next.7
+ vscode-uri: 3.0.3
+ dev: true
+
+ /@volar/source-map/0.33.6:
+ resolution: {integrity: sha512-q6J1+DN5McTvFsDeplTzTGdzsW4y1c/yeJvxqP74mUeofSFAswMo+zkSBDjupG9ufaa3LTLkkWHXw0UKtgA/cw==}
+ dev: true
+
+ /@volar/transforms/0.33.6:
+ resolution: {integrity: sha512-mdKjUP8jMiL5rHrkpNf9JbCEaSwIlzP4z68t66UhqR0x/m53uXvVCOA6Pw05WOHuHqy2iFhr93+7UC7bM669YA==}
+ dependencies:
+ '@volar/shared': 0.33.6
+ vscode-languageserver-types: 3.17.0-next.9
+ dev: true
+
+ /@volar/vue-code-gen/0.33.6:
+ resolution: {integrity: sha512-NNhviw1hVhmCgEJuYV+MCu6GFfX9skkuJObhtBFxnZ7upx0z6r1WCLrrlVkaNiPpb53aAVFbLbAyHauODZftDA==}
+ dependencies:
+ '@volar/code-gen': 0.33.6
+ '@volar/source-map': 0.33.6
+ '@vue/compiler-core': 3.2.31
+ '@vue/compiler-dom': 3.2.31
+ '@vue/shared': 3.2.31
+ dev: true
+
+ /@volar/vue-typescript/0.33.6:
+ resolution: {integrity: sha512-GSU+xpshriRriTukBUYVcWXwz9cYCfR6SaWgiVfptm78jrav1pvrTphzndJxt9J0X+SNvU2zm8jTWsda+Hxppw==}
+ dependencies:
+ '@volar/code-gen': 0.33.6
+ '@volar/pug-language-service': 0.33.6
+ '@volar/source-map': 0.33.6
+ '@volar/vue-code-gen': 0.33.6
+ '@vue/compiler-sfc': 3.2.31
+ '@vue/reactivity': 3.2.31
+ dev: true
+
/@vue/babel-helper-vue-jsx-merge-props/1.2.1:
resolution: {integrity: sha512-QOi5OW45e2R20VygMSNhyQHvpdUwQZqGPc748JLGCYEy+yp8fNFNdbNIGAgZmi9e+2JHPd6i6idRuqivyicIkA==}
dev: false
@@ -5995,6 +6058,27 @@ packages:
magic-string: 0.25.9
dev: true
+ /@vue/reactivity/3.2.31:
+ resolution: {integrity: sha512-HVr0l211gbhpEKYr2hYe7hRsV91uIVGFYNHj73njbARVGHQvIojkImKMaZNDdoDZOIkMsBc9a1sMqR+WZwfSCw==}
+ dependencies:
+ '@vue/shared': 3.2.31
+ dev: true
+
+ /@vue/runtime-core/3.2.31:
+ resolution: {integrity: sha512-Kcog5XmSY7VHFEMuk4+Gap8gUssYMZ2+w+cmGI6OpZWYOEIcbE0TPzzPHi+8XTzAgx1w/ZxDFcXhZeXN5eKWsA==}
+ dependencies:
+ '@vue/reactivity': 3.2.31
+ '@vue/shared': 3.2.31
+ dev: true
+
+ /@vue/runtime-dom/3.2.31:
+ resolution: {integrity: sha512-N+o0sICVLScUjfLG7u9u5XCjvmsexAiPt17GNnaWHJUfsKed5e85/A3SWgKxzlxx2SW/Hw7RQxzxbXez9PtY3g==}
+ dependencies:
+ '@vue/runtime-core': 3.2.31
+ '@vue/shared': 3.2.31
+ csstype: 2.6.20
+ dev: true
+
/@vue/shared/3.2.31:
resolution: {integrity: sha512-ymN2pj6zEjiKJZbrf98UM2pfDd6F2H7ksKw7NDt/ZZ1fh5Ei39X5tABugtT03ZRlWd9imccoK0hE8hpjpU7irQ==}
dev: true
@@ -17480,7 +17564,6 @@ packages:
/upath/2.0.1:
resolution: {integrity: sha512-1uEe95xksV1O0CYKXo8vQvN1JEbtJp7lb7C5U9HMsIp6IVwntkH/oNUzyVNQSd4S1sYk2FpSSW44FqMc8qee5w==}
engines: {node: '>=4'}
- dev: false
/upper-case/1.1.3:
resolution: {integrity: sha1-9rRQHC7EzdJrp4vnIilh3ndiFZg=}
@@ -17713,6 +17796,23 @@ packages:
engines: {node: '>=0.10.0'}
dev: true
+ /vscode-jsonrpc/8.0.0-next.7:
+ resolution: {integrity: sha512-JX/F31LEsims0dAlOTKFE4E+AJMiJvdRSRViifFJSqSN7EzeYyWlfuDchF7g91oRNPZOIWfibTkDf3/UMsQGzQ==}
+ engines: {node: '>=14.0.0'}
+ dev: true
+
+ /vscode-languageserver-textdocument/1.0.4:
+ resolution: {integrity: sha512-/xhqXP/2A2RSs+J8JNXpiiNVvvNM0oTosNVmQnunlKvq9o4mupHOBAnnzH0lwIPKazXKvAKsVp1kr+H/K4lgoQ==}
+ dev: true
+
+ /vscode-languageserver-types/3.17.0-next.9:
+ resolution: {integrity: sha512-9/PeDNPYduaoXRUzYpqmu4ZV9L01HGo0wH9FUt+sSHR7IXwA7xoXBfNUlv8gB9H0D2WwEmMomSy1NmhjKQyn3A==}
+ dev: true
+
+ /vscode-uri/3.0.3:
+ resolution: {integrity: sha512-EcswR2S8bpR7fD0YPeS7r2xXExrScVMxg4MedACaWHEtx9ftCF/qHG1xGkolzTPcEmjTavCQgbVzHUIdTMzFGA==}
+ dev: true
+
/vue-client-only/2.1.0:
resolution: {integrity: sha512-vKl1skEKn8EK9f8P2ZzhRnuaRHLHrlt1sbRmazlvsx6EiC3A8oWF8YCBrMJzoN+W3OnElwIGbVjsx6/xelY1AA==}
dev: false
@@ -17944,6 +18044,16 @@ packages:
/vue-template-es2015-compiler/1.9.1:
resolution: {integrity: sha512-4gDntzrifFnCEvyoO8PqyJDmguXgVPxKiIxrBKjIowvL9l+N66196+72XVYR8BBf1Uv1Fgt3bGevJ+sEmxfZzw==}
+ /vue-tsc/[email protected]:
+ resolution: {integrity: sha512-q1Fa1XudGoh3CB3vCbeRef7nlTTrEvMNrzCnk7gx7oU3biP+IpUKdfF9rrIWctNvZwYK5u0oEWjKG65p1aGcWg==}
+ hasBin: true
+ peerDependencies:
+ typescript: '*'
+ dependencies:
+ '@volar/vue-typescript': 0.33.6
+ typescript: 4.6.2
+ dev: true
+
/vue/2.6.14:
resolution: {integrity: sha512-x2284lgYvjOMj3Za7kqzRcUSxBboHqtgRE2zlos1qWaOye5yUmHn42LB1250NJBLRwEcdrB0JRwyPTEPhfQjiQ==}
dev: false
diff --git a/src/components/VLink.vue b/src/components/VLink.vue
index 01f22a5c7..33878a12a 100644
--- a/src/components/VLink.vue
+++ b/src/components/VLink.vue
@@ -4,7 +4,7 @@
/></Component>
</template>
-<script>
+<script lang="ts">
/**
* This is a wrapper component for all links. If a link is dynamically generated and doesn't have
* an `href` prop (as the links for detail pages when the image detail hasn't loaded yet),
@@ -16,14 +16,18 @@
*/
import { computed, defineComponent, useContext } from '@nuxtjs/composition-api'
-const defaultProps = { target: '_blank', rel: 'noopener noreferrer' }
+const defaultProps = Object.freeze({
+ target: '_blank',
+ rel: 'noopener noreferrer',
+})
export default defineComponent({
name: 'VLink',
props: {
href: {
type: String,
- validator: (v) =>
+ required: false,
+ validator: (v: unknown) =>
(typeof v === 'string' && v.length > 0) || typeof v === 'undefined',
},
},
@@ -33,7 +37,7 @@ export default defineComponent({
() => typeof props.href === 'string' && !['', '#'].includes(props.href)
)
const isInternal = computed(
- () => hasHref.value && props.href.startsWith('/')
+ () => hasHref.value && props.href?.startsWith('/')
)
const linkComponent = computed(() =>
hasHref.value ? (isInternal.value ? 'NuxtLink' : 'a') : 'span'
diff --git a/tsconfig.json b/tsconfig.json
index 25d0d7185..605ba4636 100644
--- a/tsconfig.json
+++ b/tsconfig.json
@@ -25,6 +25,7 @@
/* Module Resolution Options */
"moduleResolution": "Node",
"resolveJsonModule": true,
+ "types": ["@types/jest"],
"typeRoots": ["./typings", "./node_modules/@types"],
"paths": {
"~/*": ["./src/*"],
@@ -35,6 +36,7 @@
"include": [
"**/*.ts",
"test/proxy.js",
+ "src/components/VLink.vue",
"src/composables/types.js",
"src/composables/use-event-listener-outside.js",
"src/composables/use-active-audio.js",
@@ -47,5 +49,11 @@
"src/data/api-service.js",
"src/utils/deep-freeze.js"
],
- "exclude": ["node_modules"]
+ "exclude": ["node_modules"],
+ "vueCompilerOptions": {
+ "experimentalCompatMode": 2,
+ "experimentalTemplateCompilerOptions": {
+ "compatConfig": { "MODE": 2 } // optional
+ }
+ }
}