Types: refactored ts_test.
diff --git a/auto/make b/auto/make
index 5c910a7..b66864e 100644
--- a/auto/make
+++ b/auto/make
@@ -248,6 +248,9 @@
 njs_ts_deps=`echo $NJS_TS_SRCS \
         | sed -e "s# *\([^ ][^ ]*\)#\1$njs_regex_cont#g"`
 
+njs_test_ts_deps=`echo $NJS_TEST_TS_SRCS \
+        | sed -e "s# *\([^ ][^ ]*\)#\1$njs_regex_cont#g"`
+
 cat << END >> $NJS_MAKEFILE
 
 $NJS_BUILD_DIR/ts/package.json: $njs_ts_deps
@@ -270,8 +273,20 @@
 ts_lint: $NJS_BUILD_DIR/ts/node_modules
 	cd $NJS_BUILD_DIR/ts && \$(NPM) run lint
 
-ts_test: ts
-	tsc ./test/ts/test.ts
+$NJS_BUILD_DIR/test/ts/package.json: $njs_test_ts_deps
+	mkdir -p $NJS_BUILD_DIR/test
+	cp -fr test/ts $NJS_BUILD_DIR/test/
+
+$NJS_BUILD_DIR/test/ts/node_modules: \\
+	$NJS_BUILD_DIR/njs-types-\$(NJS_TYPES_VER).tgz \\
+	$NJS_BUILD_DIR/test/ts/package.json
+	cd $NJS_BUILD_DIR/test/ts && \$(NPM) install \\
+		--save-dev file:../../njs-types-\$(NJS_TYPES_VER).tgz
+	cd $NJS_BUILD_DIR/test/ts && \$(NPM) install
+	touch $NJS_BUILD_DIR/test/ts/node_modules
+
+ts_test: $NJS_BUILD_DIR/test/ts/node_modules
+	cd $NJS_BUILD_DIR/test/ts && \$(NPM) test
 
 ts_publish: ts_clean $NJS_BUILD_DIR/njs-types-\$(NJS_TYPES_VER).tgz
 	cd $NJS_BUILD_DIR/ && \$(NPM) publish njs-types-\$(NJS_TYPES_VER).tgz
diff --git a/auto/sources b/auto/sources
index b7c0a46..0c59df9 100644
--- a/auto/sources
+++ b/auto/sources
@@ -73,3 +73,5 @@
 "
 
 NJS_TS_SRCS=$(find ts/ -name "*.d.ts" -o -name "*.json")
+
+NJS_TEST_TS_SRCS=$(find test/ts/ -name "*.ts" -o -name "*.json")
diff --git a/test/ts/package.json b/test/ts/package.json
new file mode 100644
index 0000000..3daeea8
--- /dev/null
+++ b/test/ts/package.json
@@ -0,0 +1,15 @@
+{
+  "private": true,
+  "name": "njs-types-test",
+  "version": "0.0.0",
+  "description": "Tests for njs TypeScript type definitions.",
+  "scripts": {
+    "test": "tsc"
+  },
+  "author": "NGINX, Inc.",
+  "license": "BSD-2-Clause",
+  "devDependencies": {
+    "njs-types": "file:../../ts",
+    "typescript": "~4.0.3"
+  }
+}
diff --git a/test/ts/test.ts b/test/ts/test.ts
index dbb8df7..ce7940d 100644
--- a/test/ts/test.ts
+++ b/test/ts/test.ts
@@ -1,8 +1,3 @@
-/// <reference path="../../build/ts/ngx_http_js_module.d.ts" />
-/// <reference path="../../build/ts/fs.d.ts" />
-/// <reference path="../../build/ts/querystring.d.ts" />
-/// <reference path="../../build/ts/crypto.d.ts" />
-
 import fs from 'fs';
 import qs from 'querystring';
 import crypto from 'crypto';
@@ -15,9 +10,9 @@
 
     s = 'ordinary string';
     bs = String.bytesFrom('000000', 'hex');
-    bs = s.toBytes();
+    var bs2: NjsByteString | null = s.toBytes();
     bs = s.toUTF8();
-    bs.fromBytes(null, null);
+    bs.fromBytes(undefined, undefined);
 
     s = bs + '';
 
@@ -30,29 +25,30 @@
 
     bs = r.args.x;
     bs = r.args[1];
-    s = r.args.x.fromUTF8();
+    var s2: string | null = r.args.x.fromUTF8();
     s = r.args.x + '';
 
     // r.headersIn
 
-    r.headersIn['Accept'].fromBytes() == 'dddd';
+    r.headersIn['Accept']?.fromBytes() == 'dddd';
 
     // r.headersOut
 
     r.headersOut['Content-Type'] = 'text/plain';
     // Warning: r.headersOut['Content-Type'] = ['a', 'b'];
     r.headersOut['Connection'] = undefined;
-    r.headersOut['Connection'] = null;
+
+    delete r.headersOut['Bar'];
 
     r.headersOut['Set-Cookie'] = ['aaa', 'bbb'];
     r.headersOut['Foo'] = ['aaa', 'bbb'];
 
-    r.subrequest('/uri', reply => r.return(200, reply.headersOut["Location"]));
+    r.subrequest('/uri', reply => r.return(200, reply.headersOut["Location"] ?? ''));
 
     // r.log
 
     r.log(bs);
-    r.log(r.headersOut['Connection']);
+    r.log(r.headersOut['Connection'] ?? '');
 
     // r.variables
 
@@ -64,7 +60,7 @@
     r.subrequest('/p/sub2', reply => r.return(reply.status));
     r.subrequest('/p/sub3', {detached:true});
     r.subrequest('/p/sub4', 'a=1&b=2').then(reply => r.return(reply.status,
-                                        JSON.stringify(JSON.parse(reply.responseBody))));
+                                        JSON.stringify(JSON.parse(reply.responseBody ?? ''))));
 
 }
 
diff --git a/test/ts/tsconfig.json b/test/ts/tsconfig.json
new file mode 100644
index 0000000..f11f689
--- /dev/null
+++ b/test/ts/tsconfig.json
@@ -0,0 +1,35 @@
+{
+    "compilerOptions": {
+        "target": "ES5",
+        "module": "es2015",
+        "lib": [
+            "ES2015",
+            "ES2016.Array.Include",
+            "ES2017.Object",
+            "ES2017.String"
+        ],
+        "noEmit": true,
+        "downlevelIteration": true,
+
+        "strict": true,
+        "noImplicitAny": true,
+        "strictNullChecks": true,
+        "strictFunctionTypes": true,
+        "strictBindCallApply": true,
+        "strictPropertyInitialization": true,
+        "noImplicitThis": true,
+        "alwaysStrict": true,
+
+        "moduleResolution": "node",
+        "forceConsistentCasingInFileNames": true
+    },
+    "include": [
+        "**/*.ts"
+    ],
+    "exclude": [
+        "./node_modules/**"
+    ],
+    "files": [
+        "./node_modules/njs-types/ngx_http_js_module.d.ts"
+    ]
+}