From f578f281b03229f2f6dbf3e220a8e77c0a91bc6d Mon Sep 17 00:00:00 2001 From: Rafal Chlodnicki Date: Thu, 30 Mar 2023 21:49:15 +0200 Subject: [PATCH] chore: minor internal type fixes --- example/components/Author.vue | 6 ++++-- example/nuxt.config.js | 6 ++++-- example/package.json | 4 +++- example/pages/index.vue | 12 +++++++----- example/pages/posts/_id.vue | 2 +- example/plugins/vue-placeholders.js | 1 + example/tsconfig.json | 26 ++++++++++++++++++++++++++ example/types/vue.d.ts | 5 +++++ package.json | 2 +- test/fixture/api/posts.js | 3 ++- test/fixture/components/comp.vue | 4 ++++ test/fixture/pages/ttfb.vue | 6 ++++-- test/fixture/utils/index.js | 4 ++++ tsconfig.json | 11 +++++++++-- yarn.lock | 8 ++++---- 15 files changed, 79 insertions(+), 21 deletions(-) create mode 100644 example/tsconfig.json create mode 100644 example/types/vue.d.ts diff --git a/example/components/Author.vue b/example/components/Author.vue index 9342d473..9aa4ae3d 100644 --- a/example/components/Author.vue +++ b/example/components/Author.vue @@ -18,6 +18,8 @@ import { ref, } from '@nuxtjs/composition-api' +/** @typedef {{ name: number }} User */ + export default defineComponent({ props: { userId: { @@ -26,10 +28,10 @@ export default defineComponent({ }, }, setup(props) { - const user = ref({}) + const user = ref(/** @type {User} */({})) const { $http } = useContext() - + useFetch(async () => { user.value = await $http.$get( `https://jsonplaceholder.typicode.com/users/${props.userId}` diff --git a/example/nuxt.config.js b/example/nuxt.config.js index 0f07005e..acb4ea94 100644 --- a/example/nuxt.config.js +++ b/example/nuxt.config.js @@ -1,6 +1,7 @@ -const fetch = require('node-fetch') +import fetch from 'node-fetch' const serverlessEnvironment = !!process.env.NOW_BUILD +/** @type {import('@nuxt/types').NuxtConfig} */ export default { server: { port: process.env.PORT || 8000, @@ -20,6 +21,7 @@ export default { generate: { interval: 2000, async routes() { + /** @type {{ userId: number, id: number, title: string, body: string }[]} */ const posts = await fetch('https://jsonplaceholder.typicode.com/posts') .then(res => res.json()) .then(d => d.slice(0, 20)) @@ -27,6 +29,6 @@ export default { return ['/'].concat(routes) }, - exclude: ['/posts/23'] + exclude: [RegExp('/posts/23')] }, } diff --git a/example/package.json b/example/package.json index 489b1ea3..b9ae6b87 100644 --- a/example/package.json +++ b/example/package.json @@ -6,7 +6,9 @@ "vue-content-placeholders": "^0.2.1" }, "devDependencies": { - "nuxt": "latest" + "@nuxt/types": "^2.16.3", + "@types/node-fetch": "^2.6.3", + "nuxt": "^2.16.3" }, "scripts": { "dev": "nuxt", diff --git a/example/pages/index.vue b/example/pages/index.vue index c853fb79..1a960a49 100644 --- a/example/pages/index.vue +++ b/example/pages/index.vue @@ -34,16 +34,18 @@ import { ref, } from '@nuxtjs/composition-api' +/** @typedef {{ userId: number, id: number, title: string, body: string }} Post */ + export default defineComponent({ setup() { - const posts = ref(null) + const posts = ref(/** @type {Post[]} */([])) const { $http } = useContext() - + useFetch(async () => { - posts.value = await $http - .$get('https://jsonplaceholder.typicode.com/posts') - .then(posts => posts.slice(0, 20)) + /** @type {Post[]} */ + const data = await $http.$get('https://jsonplaceholder.typicode.com/posts') + posts.value = data.slice(0, 20) }) return { posts } diff --git a/example/pages/posts/_id.vue b/example/pages/posts/_id.vue index cc87e521..4e5d40b3 100644 --- a/example/pages/posts/_id.vue +++ b/example/pages/posts/_id.vue @@ -53,7 +53,7 @@ export default defineComponent({ post.value = await $http.$get( `https://jsonplaceholder.typicode.com/posts/${params.value.id}` ) - }) + }) return { post } }, diff --git a/example/plugins/vue-placeholders.js b/example/plugins/vue-placeholders.js index 40bfc7e1..e484228b 100644 --- a/example/plugins/vue-placeholders.js +++ b/example/plugins/vue-placeholders.js @@ -1,4 +1,5 @@ import Vue from 'vue' +// @ts-ignore import VueContentPlaceholders from 'vue-content-placeholders' Vue.use(VueContentPlaceholders) diff --git a/example/tsconfig.json b/example/tsconfig.json new file mode 100644 index 00000000..947bb87c --- /dev/null +++ b/example/tsconfig.json @@ -0,0 +1,26 @@ +{ + "compilerOptions": { + "baseUrl": ".", + "rootDir": ".", + "strict": true, + "sourceMap": true, + "allowJs": true, + "checkJs": true, + "noEmit": true, + "target": "ESNext", + "module": "CommonJS", + "moduleResolution": "Node", + "lib": [ + "ESNext", + "DOM" + ], + "esModuleInterop": true, + "resolveJsonModule": true, + "allowSyntheticDefaultImports": true, + "types": [ + "node", + "@nuxt/http", + "@nuxt/types" + ] + }, +} diff --git a/example/types/vue.d.ts b/example/types/vue.d.ts new file mode 100644 index 00000000..ac1ded79 --- /dev/null +++ b/example/types/vue.d.ts @@ -0,0 +1,5 @@ +declare module '*.vue' { + import { DefineComponent } from 'vue' + const component: DefineComponent<{}, {}, any> + export default component +} diff --git a/package.json b/package.json index 2751119c..372e0a6a 100644 --- a/package.json +++ b/package.json @@ -100,7 +100,7 @@ "testcafe": "1.19.0", "ts-loader": "^8.4.0", "tsd": "^0.25.0", - "typescript": "4.9.4", + "typescript": "4.9.5", "vitest": "^0.25.7", "yorkie": "^2.0.0" }, diff --git a/test/fixture/api/posts.js b/test/fixture/api/posts.js index f0f4ee70..1dc1207b 100644 --- a/test/fixture/api/posts.js +++ b/test/fixture/api/posts.js @@ -1,4 +1,5 @@ +/** @type {import('@nuxt/types').ServerMiddleware} */ module.exports = (req, res) => { res.setHeader('Content-Type', 'application/json') - return res.end(JSON.stringify({ id: req.url.split('/').slice(-1)[0] })) + res.end(JSON.stringify({ id: req.url?.split('/').slice(-1)[0] })) } diff --git a/test/fixture/components/comp.vue b/test/fixture/components/comp.vue index cd47c950..ecdb0014 100644 --- a/test/fixture/components/comp.vue +++ b/test/fixture/components/comp.vue @@ -7,6 +7,10 @@