6
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Nuxt.jsのnuxt.config.jsをtsファイルに変更して型定義を追加する方法【nuxt.config.ts】

Last updated at Posted at 2021-06-03

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を使用するのが良さそうです。

参考

6
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?