# HG changeset patch # User Yaroslav Zhuravlev # Date 1589556163 -3600 # Node ID 0e6bbd8138c4e1cfe996b01b09fdd7fd2ad8ed78 # Parent b686736680e3be4eb24a73ba1e844301847dd7f1 Added article about TypeScript in njs. diff -r b686736680e3 -r 0e6bbd8138c4 xml/en/GNUmakefile --- a/xml/en/GNUmakefile Tue May 12 22:06:12 2020 +0100 +++ b/xml/en/GNUmakefile Fri May 15 16:22:43 2020 +0100 @@ -130,6 +130,7 @@ njs/install \ njs/reference \ njs/node_modules \ + njs/typescript \ TOP = \ download \ diff -r b686736680e3 -r 0e6bbd8138c4 xml/en/docs/njs/index.xml --- a/xml/en/docs/njs/index.xml Tue May 12 22:06:12 2020 +0100 +++ b/xml/en/docs/njs/index.xml Fri May 15 16:22:43 2020 +0100 @@ -9,7 +9,7 @@
@@ -50,10 +50,6 @@ - - - - @@ -80,6 +76,20 @@ + + + + + + + + + + + + + +
diff -r b686736680e3 -r 0e6bbd8138c4 xml/en/docs/njs/typescript.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/xml/en/docs/njs/typescript.xml Fri May 15 16:22:43 2020 +0100 @@ -0,0 +1,114 @@ + + + + + + +
+ +
+ + +TypeScript is +a typed superset of JavaScript +that compiles to plain JavaScript. + + + +TypeScript supports definition files that contain +type information of existing JavaScript libraries. +This enables other programs to use the values defined in the files +as if they were statically typed TypeScript entities. + + + +njs provides TypeScript definition files for its +API which can be used to: + + + +Get autocompletion and API check in an editor + + + +Write njs type-safe code + + + + + +
+ + +
+ + + +$ 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 + + + +
+ + +
+ + +Put *.d.ts files to a place where you editor can find it. + + + +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"); +} + + + +
+ + +
+ + +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 installation: + +# npm install -g typescript + +TypeScript compilation: + +$ tsc test.ts +$ cat test.js + +The resulting test.js file can be used directly with njs. + + +
+ + +
diff -r b686736680e3 -r 0e6bbd8138c4 xml/ru/GNUmakefile --- a/xml/ru/GNUmakefile Tue May 12 22:06:12 2020 +0100 +++ b/xml/ru/GNUmakefile Fri May 15 16:22:43 2020 +0100 @@ -112,6 +112,7 @@ njs/examples \ njs/install \ njs/reference \ + njs/typescript \ TOP = \ download \ diff -r b686736680e3 -r 0e6bbd8138c4 xml/ru/docs/njs/index.xml --- a/xml/ru/docs/njs/index.xml Tue May 12 22:06:12 2020 +0100 +++ b/xml/ru/docs/njs/index.xml Fri May 15 16:22:43 2020 +0100 @@ -9,7 +9,7 @@
@@ -51,10 +51,6 @@ -Использование модулей Node.js в njs - - - @@ -81,6 +77,20 @@ + + + + + + + + +Использование модулей Node.js в njs + + + + +
diff -r b686736680e3 -r 0e6bbd8138c4 xml/ru/docs/njs/typescript.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/xml/ru/docs/njs/typescript.xml Fri May 15 16:22:43 2020 +0100 @@ -0,0 +1,114 @@ + + + + + + +
+ +
+ + +TypeScript—это +типизированное подмножество JavaScript, +которое компилируется в обычный JavaScript. + + + +TypeScript поддерживает файлы деклараций, в которых содержится +типизированная информация существующих библиотек JavaScript. +С их помощью программы могут использовать значения в файлах также, +если бы эти значения были статически типизированными сущностями TypeScript. + + + +В njs файлы деклараций TypeScript предоставляются для +API и могут использоваться при: + + + +автозаполнении и проверки API в редакторе + + + +создании типобезопасного njs-кода. + + + + + +
+ + +
+ + + +$ 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 + + + +
+ + +
+ + +Файлы деклараций *.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"); +} + + + +
+ + +
+ + +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. + + +
+ +