TypeScript 정의 파일을 사용하여 njs 코드 작성
TypeScript는 일반 JavaScript에 컴파일되는 JavaScript의 형식이 있는 상위 집합입니다.
TypeScript는 기존 JavaScript 라이브러리의 형식 정보가 들어 있는 정의 파일을 지원합니다. 이에 따라 다른 프로그램이 파일에 정의된 값을 정적으로 형식이 있는 TypeScript 엔터티인 것처럼 사용할 수 있습니다.
njs는 다음에 사용할 수 있는 해당 API에 대한 TypeScript 정의 파일을 제공합니다.
- 편집기에서 자동 완성 및 API 검사 가져오기
- njs 형식 안전 코드 작성
TypeScript 정의 파일 컴파일
$ hg clone http://hg.nginx.org/njs
$ cd njs && ./configure && make ts
$ ls build/ts/
njs_core.d.ts
njs_shell.d.ts
ngx_http_js_module.d.ts
ngx_stream_js_module.d.ts
API 확인 및 자동 완성
*.d.ts
편집자가 찾을 수 있는 위치에 파일을 두십시오 .
test.js:
/// <reference path="ngx_http_js_module.d.ts" />
/**
* @param {NginxHTTPRequest} r
* */
function content_handler(r) {
r.headersOut['content-type'] = 'text/plain';
r.return(200, "Hello");
}
njs 유형 안전 코드 작성
test.ts
:
/// <reference path="ngx_http_js_module.d.ts" />
function content_handler(r: NginxHTTPRequest) {
r.headersOut['content-type'] = 'text/plain';
r.return(200, "Hello from TypeScript");
}
TypeScript 설치:
# npm install -g typescript
TypeScript 컴파일:
$ tsc test.ts
$ cat test.js
결과 test.js
파일은 njs와 함께 직접 사용할 수 있습니다.