Recent Posts
Recent Comments
Link
«   2025/02   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28
Archives
Today
Total
관리 메뉴

kohigowild

Property 'env' does not exist on type 'ImportMeta' 본문

JavaScript/TypeScript

Property 'env' does not exist on type 'ImportMeta'

kohi ☕ 2023. 3. 26. 03:10

Problem

타입스크립트 환경에서 env 변수 사용 시 import.meta.env에서 타입 오류 발생

  •  Property 'env' does not exist on type 'ImportMeta'.

 

Reference

https://github.com/vitejs/vite/issues/9539

 

Property 'env' does not exist on type 'ImportMeta' in 3.0.0 · Issue #9539 · vitejs/vite

Describe the bug In vite 3.0.0 it still exists, even if it was previously closed. Can be recreated using react-ts template. Try to use env variable via import.meta.env, TS will give you an error Pr...

github.com

 

Try to solve

  • .env.d.ts 파일 생성 후 타입 정의
interface ImportMeta {
    env: {
        VITE_REST_API_KEY: string;
    };
}

 

  • tsconfig 추가
"types": ["vite/client"]

'JavaScript > TypeScript' 카테고리의 다른 글

Typescript 기본 타입 정리 (primitive types)  (0) 2023.01.05