[study] vue/cli vue3 + typeScript ์„ธํŒ… / shims-vue.d.ts

JooSehyunยท2024๋…„ 9์›” 5์ผ
0

[Study]

๋ชฉ๋ก ๋ณด๊ธฐ
42/56
post-thumbnail

[study] vue/cli vue3 + typeScript ์ดˆ๊ธฐ ์„ธํŒ… / shims-vue.d.ts


๐Ÿ•ต๏ธshims-vue.d.ts ์ด๋ž€?

shims-vue.d.ts ํŒŒ์ผ์€ TypeScript ํ”„๋กœ์ ํŠธ์—์„œ Vue ํŒŒ์ผ(*.vue)์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ฃผ๋Š” ์„ ์–ธ ํŒŒ์ผ์ž…๋‹ˆ๋‹ค. TypeScript๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ .vue ํŒŒ์ผ์„ ์ธ์‹ํ•˜์ง€ ๋ชปํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ์ด ํŒŒ์ผ์„ ํ†ตํ•ด TypeScript์—๊ฒŒ .vue ํŒŒ์ผ์˜ ๋ชจ๋“ˆ์„ ์–ด๋–ป๊ฒŒ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•˜๋Š”์ง€ ์•Œ๋ ค์ค๋‹ˆ๋‹ค.


๊ธฐ๋ณธ ๊ฐ’

/* eslint-disable */
declare module '*.vue' {
  import type { DefineComponent } from 'vue'
  const component: DefineComponent<{}, {}, any>
  export default component
}

vue/cli ๋กœ vue3 + typeScript ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•˜๋ฉด ๋ฃจํŠธ๋””๋ ‰ํ† ๋ฆฌ์—

shims-vue.d.ts ํŒŒ์ผ์ด ์ƒ์„ฑ

1๏ธโƒฃ  declare module '*.vue' {
2๏ธโƒฃ    import type { DefineComponent } from 'vue'
3๏ธโƒฃ    const component: DefineComponent<{}, {}, any>
4๏ธโƒฃ    export default component
  	}

  1. declare module '*.vue' { ... }:

์ด ๊ตฌ๋ฌธ์€ ๋ชจ๋“  .vue ํŒŒ์ผ์„ ๋ชจ๋“ˆ๋กœ ์„ ์–ธํ•ฉ๋‹ˆ๋‹ค. TypeScript๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ .vue ํŒŒ์ผ์„ ์ธ์‹ํ•˜์ง€ ๋ชปํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ์ด ์„ ์–ธ์„ ํ†ตํ•ด .vue ํŒŒ์ผ์„ ๋ชจ๋“ˆ๋กœ ์ทจ๊ธ‰ํ•˜๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.


  1. import type { DefineComponent } from 'vue':

Vue 3์—์„œ ์ œ๊ณตํ•˜๋Š” DefineComponent ํƒ€์ž…์„ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. DefineComponent๋Š” Vue ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ •์˜ํ•˜๋Š” ํƒ€์ž…์ž…๋‹ˆ๋‹ค.
import type ๊ตฌ๋ฌธ์€ ํƒ€์ž…๋งŒ ๊ฐ€์ ธ์˜ค๊ฒ ๋‹ค๋Š” ์˜๋ฏธ๋กœ, ๋Ÿฐํƒ€์ž„์— ์˜ํ–ฅ์„ ๋ฏธ์น˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.


  1. const component: DefineComponent<{}, {}, any>:

๋ชจ๋“  .vue ํŒŒ์ผ์ด DefineComponent ํƒ€์ž…์˜ ์ปดํฌ๋„ŒํŠธ์ž„์„ ๋ช…์‹œํ•ฉ๋‹ˆ๋‹ค.
DefineComponent<{}, {}, any>๋Š” ์ œ๋„ค๋ฆญ ํƒ€์ž…์œผ๋กœ, ์ฒซ ๋ฒˆ์งธ์™€ ๋‘ ๋ฒˆ์งธ ์ธ์ž๋Š” ์ปดํฌ๋„ŒํŠธ์˜ props์™€ ์ƒํƒœ๋ฅผ ๋‚˜ํƒ€๋‚ด๋ฉฐ, ์—ฌ๊ธฐ์„œ๋Š” ๋นˆ ๊ฐ์ฒด๋กœ ์„ค์ •๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์„ธ ๋ฒˆ์งธ ์ธ์ž๋Š” ์ปดํฌ๋„ŒํŠธ์˜ ๋ฉ”์„œ๋“œ ํƒ€์ž…์„ ๋‚˜ํƒ€๋‚ด๋ฉฐ, any๋กœ ์„ค์ •๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.

๊ฐ ์ œ๋„ค๋ฆญ ์ธ์ž๋Š” ์ปดํฌ๋„ŒํŠธ์˜ props , emits , slots ๋“ฑ์„ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.


๐Ÿ•ต๏ธ๋” ์—„๊ฒฉํ•œ ํƒ€์ž… ๊ฒ€์‚ฌ๋ฅผ ํ•˜๋ ค๋ฉด?

const component: DefineComponent<NonNullable<unknown>, NonNullable<unknown>, any>;
  1. ์ฒซ ๋ฒˆ์งธ ์ธ์ž: NonNullable<unknown>
    NonNullable<unknown>๋Š” unknown ํƒ€์ž…์—์„œ null๊ณผ undefined๋ฅผ ์ œ์™ธํ•œ ํƒ€์ž…์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ์ด ์ปดํฌ๋„ŒํŠธ์˜ props๋Š” null์ด๋‚˜ undefined๊ฐ€ ๋  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

  2. ๋‘ ๋ฒˆ์งธ ์ธ์ž: NonNullable<unknown>
    ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ, setup ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜๊ฐ’๋„ null์ด๋‚˜ undefined๊ฐ€ ๋  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

  3. ์„ธ ๋ฒˆ์งธ ์ธ์ž: any
    ์ปดํฌ๋„ŒํŠธ์˜ ๋ฐ์ดํ„ฐ, ๋ฉ”์„œ๋“œ, ์ปดํ“จํ‹ฐ๋“œ ์†์„ฑ ๋“ฑ์„ ํฌํ•จํ•˜๋Š” ํƒ€์ž…์„ ์ •์˜ํ•˜๋ฉฐ, ์—ฌ๊ธฐ์„œ๋Š” any๋กœ ์„ค์ •๋˜์–ด ์žˆ์–ด ์–ด๋–ค ํƒ€์ž…์ด๋“  ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


  1. export default component:

์ด ์ปดํฌ๋„ŒํŠธ๋ฅผ ๊ธฐ๋ณธ ๋‚ด๋ณด๋‚ด๊ธฐ๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด .vue ํŒŒ์ผ์„ importํ•  ๋•Œ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ด ์ปดํฌ๋„ŒํŠธ๋ฅผ ๊ฐ€์ ธ์˜ค๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.


0๊ฐœ์˜ ๋Œ“๊ธ€