-
[TypeScript] NodeJs 의 global(전역 객체) 사용언어/TypeScript 2022. 9. 3. 00:43
NodeJS에 내장되어있는 Global 객체.
global.(변수이름)으로 선언을 하면 전역에서 사용할 수 있다
에러메세지
Element implicitly has an 'any' type because type 'typeof globalThis' has no index signature
(js로 만든 서버를 ts로 변경할 때 겪었던 첫 문제... ts가 어렵게 느껴진 순간이었다.)
해결 방법
declare global { var __base: string; } global.__base = __dirname + '/';
위와 같이 선언할 변수의 타입을 정해주고 사용하면 된다!
declare ? 이미 선언되어있는 타입에 선언을 추가하거나 재정의할 때 사용한다.
참고문서
https://www.typescriptlang.org/docs/handbook/declaration-merging.html#global-augmentation
https://nodejs.org/api/globals.html
https://stackoverflow.com/questions/35074713/extending-typescript-global-object-in-node-js
'언어 > TypeScript' 카테고리의 다른 글
[Typescript] 타입스크립트란? (0) 2022.05.29