TypeScript導入時などに、nuxt.config.jsをtsファイルに変更する方法になります。
nuxt.config.jsの拡張子をtsに変換
まずは、拡張子をjsからtsに変換します。
@nuxt/typesを読み込み、型定義を追加する
変更前
nuxt.config.js
export default {
mode: 'universal',
/*
** Headers of the page
*/
head: {
title: process.env.npm_package_name || '',
meta: [
{ charset: 'utf-8' },
{ name: 'viewport', content: 'width=device-width, initial-scale=1' },
{
hid: 'description',
name: 'description',
content: process.env.npm_package_description || ''
}
],
link: [{ rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }]
},
/*
** Customize the progress-bar color
*/
loading: { color: '#fff' },
/*
** Global CSS
*/
css: [],
/*
** Plugins to load before mounting the App
*/
plugins: [],
/*
** Nuxt.js dev-modules
*/
buildModules: [
'@nuxt/typescript-build',
// Doc: https://github.com/nuxt-community/nuxt-tailwindcss
'@nuxtjs/tailwindcss'
],
/*
** Nuxt.js modules
*/
modules: [
// Doc: https://axios.nuxtjs.org/usage
'@nuxtjs/axios'
],
/*
** Axios module configuration
** See https://axios.nuxtjs.org/options
*/
axios: {},
/*
** Build configuration
*/
build: {
/*
** You can extend webpack config here
*/
extend(config, ctx) {}
}
}
変更後
nuxt.config.ts
import { NuxtConfig } from '@nuxt/types'
const nuxtConfig: NuxtConfig = {
/*
** 上記と同様なので省略
*/
}
export default nuxtConfig
これで、nuxt.config.jsをtsファイルに変換することができます。
補足
@nuxt/types
のバージョンによっては、Configuration
を使用する必要があります。
nuxt.config.ts
import { Configuration } from '@nuxt/types'
const nuxtConfig: Configuration = {}
しかし、Configuration
は現在非推奨なので、可能であればNuxtConfig
を使用するのが良さそうです。
参考