comparison xml/en/docs/njs/typescript.xml @ 2541:0e6bbd8138c4

Added article about TypeScript in njs.
author Yaroslav Zhuravlev <yar@nginx.com>
date Fri, 15 May 2020 16:22:43 +0100
parents
children
comparison
equal deleted inserted replaced
2540:b686736680e3 2541:0e6bbd8138c4
1 <?xml version="1.0"?>
2
3 <!--
4 Copyright (C) Nginx, Inc.
5 -->
6
7 <!DOCTYPE article SYSTEM "../../../../dtd/article.dtd">
8
9 <article name="Writing njs code using TypeScript definition files"
10 link="/en/docs/njs/typescript.html"
11 lang="en"
12 rev="1">
13
14 <section>
15
16 <para>
17 <link url="https://www.typescriptlang.org">TypeScript</link> is
18 a typed superset of JavaScript
19 that compiles to plain JavaScript.
20 </para>
21
22 <para>
23 TypeScript supports definition files that contain
24 type information of existing JavaScript libraries.
25 This enables other programs to use the values defined in the files
26 as if they were statically typed TypeScript entities.
27 </para>
28
29 <para>
30 njs provides TypeScript definition files for its
31 <link doc="reference.xml">API</link> which can be used to:
32 <list type="bullet">
33
34 <listitem>
35 Get autocompletion and API check in an editor
36 </listitem>
37
38 <listitem>
39 Write njs type-safe code
40 </listitem>
41
42 </list>
43 </para>
44
45 </section>
46
47
48 <section id="get" name="Compiling TypeScript definition files">
49
50 <para>
51 <example>
52 $ hg clone http://hg.nginx.org/njs
53 $ cd njs &amp;&amp; ./configure &amp;&amp; make ts
54 $ ls build/ts/
55 njs_core.d.ts
56 njs_shell.d.ts
57 ngx_http_js_module.d.ts
58 ngx_stream_js_module.d.ts
59 </example>
60 </para>
61
62 </section>
63
64
65 <section id="autocomplete" name="API checks and autocompletions">
66
67 <para>
68 Put <literal>*.d.ts</literal> files to a place where you editor can find it.
69 </para>
70
71 <para>
72 <literal>test.js</literal>:
73 <example>
74 /// &lt;reference path="ngx_http_js_module.d.ts" /&gt;
75 /**
76 * @param {NginxHTTPRequest} r
77 * */
78 function content_handler(r) {
79 r.headersOut['content-type'] = 'text/plain';
80 r.return(200, "Hello");
81 }
82 </example>
83 </para>
84
85 </section>
86
87
88 <section id="write" name="Writing njs type-safe code">
89
90 <para>
91 <literal>test.ts</literal>:
92 <example>
93 /// &lt;reference path="ngx_http_js_module.d.ts" /&gt;
94 function content_handler(r: NginxHTTPRequest) {
95 r.headersOut['content-type'] = 'text/plain';
96 r.return(200, "Hello from TypeScript");
97 }
98 </example>
99 TypeScript installation:
100 <example>
101 # npm install -g typescript
102 </example>
103 TypeScript compilation:
104 <example>
105 $ tsc test.ts
106 $ cat test.js
107 </example>
108 The resulting <literal>test.js</literal> file can be used directly with njs.
109 </para>
110
111 </section>
112
113
114 </article>