Nuxt TypeScript 2 11 - ChoDragon9/posts GitHub Wiki

์„ค์น˜

์„ค์น˜ ์ˆœ์„œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

$ npm install -g npx # npx ์„ค์น˜. create-nuxt-app์„ ์„ค์น˜ํ•˜๊ธฐ ์œ„ํ•จ.
$ npx create-nuxt-app <project-name> # nuxt ์„ค์น˜
$ cd <project-name>
$ npm install --save-dev @nuxt/typescript-build

ํ™˜๊ฒฝ์„ค์ •

์„ค์น˜๊ฐ€ ์™„๋ฃŒ๋˜๋ฉด ๋ช‡๊ฐ€์ง€ ํ™˜๊ฒฝ์„ค์ •์ด ํ•„์š”ํ•˜๋‹ค.

1. TypeScript์˜ ๋นŒ๋“œ ์˜ต์…˜ ์„ค์ •

tsconfig.json ํŒŒ์ผ์„ ์ถ”๊ฐ€ํ•œ๋’ค ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•œ๋‹ค. ์•„๋ž˜ ์ฝ”๋“œ๋Š” ๊ณต์‹ ๊ฐ€์ด๋“œ ๋ฌธ์„œ์—์„œ ๊ฐ€์ ธ์™”๋‹ค.

{
  "compilerOptions": {
    "target": "es2018",
    "module": "esnext",
    "moduleResolution": "node",
    "lib": [
      "esnext",
      "esnext.asynciterable",
      "dom"
    ],
    "esModuleInterop": true,
    "allowJs": true,
    "sourceMap": true,
    "strict": true,
    "noEmit": true,
    "baseUrl": ".",
    "paths": {
      "~/*": [
        "./*"
      ],
      "@/*": [
        "./*"
      ]
    },
    "types": [
      "@types/node",
      "@nuxt/types"
    ]
  },
  "exclude": [
    "node_modules"
  ]
}

2. *.vue ํ™•์žฅ์ž ํŒŒ์ผ ์„ค์ •

TypeScript์—์„œ *.vue ํ™•์žฅ์ž ํŒŒ์ผ์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ ์„ค์ •์ด๋‹ค. vue-shim.d.ts ํŒŒ์ผ์„ ์ถ”๊ฐ€ํ•œ๋’ค ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•œ๋‹ค.

declare module "*.vue" {
  import Vue from 'vue'
  export default Vue
}

3. Nuxt์— TypeScript ๋นŒ๋“œ ์„ค์ •

nuxt.config.js์˜ buildModules์— @nuxt/typescript-build๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.

buildModules: [
+  '@nuxt/typescript-build'
],

๋™์ž‘ํ™•์ธ

๋จผ์ € ๋กœ์ปฌ ์„œ๋ฒ„๋ฅผ ๋„์šด๋‹ค.

$ npm run dev

pages/index.vue ํŒŒ์ผ์—์„œ TypeScript ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์—ฌ ํ™•์ธํ•ด๋ณด์ž.

<script lang="ts">
import Logo from '~/components/Logo.vue'

interface User {
  firstName: string
  lastName: string
}

export default {
  components: {
    Logo
  },
  data (): User {
    return {
      firstName: 'Peter',
      lastName: 'Cho'
    }
  }
}
</script>

๋

โš ๏ธ **GitHub.com Fallback** โš ๏ธ