TSC won't compile on different systems - node.js

I created a node app in typescript. When I run tsc locally everything works and I can run the app. But when I deploy on heroku, it is giving me compilation errors :
app/api/controllers/ingredient.controller.ts(3,24): error TS2307: Cannot find module '../schemas/ingredient.schema'.
app/api/controllers/ingredient.controller.ts(23,13): error TS7006: Parameter 'units' implicitly has an 'any' type.
app/api/controllers/ingredient.controller.ts(30,42): error TS7006: Parameter 'result' implicitly has an 'any' type.
app/api/controllers/ingredient.controller.ts(37,70): error TS7006: Parameter 'result' implicitly has an 'any' type.
app/api/controllers/ingredient.controller.ts(44,70): error TS7006: Parameter 'result' implicitly has an 'any' type.
The strange thing is that I made sure all versions are the same: NPM, Node, TSC ...
I have a TSConfig that is picked up and used. Where else am I supposed to look ?
Versions:
> node -v && npm -v && tsc -v && tsc
v13.3.0
6.13.1
Version 3.7.3
Package.json:
{
"name": "server",
"version": "1.0.0",
"description": "",
"main": "build/app.js",
"scripts": {
"tsc": "node -v && npm -v && tsc -v && tsc",
"watch-ts": "tsc -w",
"watch-node": "nodemon build/app.js",
"watch": "docker-compose up -d && concurrently -k -p \"[{name}]\" -n \"TypeScript, Node\" -c \"yello.bold, cyan.bold\" \"yarn run watch-ts\" \"yarn run watch-node\"",
"start": "node build/app.js",
"build": "npm run tsc"
},
"author": "",
"license": "ISC",
"dependencies": {
"#overnightjs/core": "^1.6.9",
"#overnightjs/logger": "^1.1.9",
"#types/body-parser": "^1.17.1",
"#types/cors": "^2.8.6",
"#types/dotenv": "^8.2.0",
"#types/express": "^4.17.2",
"#types/mongodb": "^3.3.11",
"#types/mongoose": "^5.5.32",
"#types/multer": "^1.3.10",
"concurrently": "^5.0.0",
"cors": "^2.8.5",
"dotenv": "^8.2.0",
"mongoose": "^5.7.13",
"multer": "^1.4.2",
"nodemon": "^2.0.1",
"typescript": "^3.7.3"
},
"engines": {
"node": "13.3.0",
"npm": "6.13.1"
}
}
TSCONFIG:
{
"compilerOptions": {
"target": "es6", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
"outDir": "build", /* Redirect output structure to the directory. */
"rootDir": "./app", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
"importHelpers": true, /* Import emit helpers from 'tslib'. */
"strict": true, /* Enable all strict type-checking options. */
"noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
/* Module Resolution Options */
"moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
"baseUrl": "./", /* Base directory to resolve non-absolute module names. */
"typeRoots": [ /* List of folders to include type definitions from. */
"node_modules/#types"
],
"types": ["node"], /* Type declaration files to be included in compilation. */
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
/* Experimental Options */
"experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
"emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
/* Advanced Options */
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
},
"include": [
"app/*"
]
}

Advice
Run tsc locally and deploy the built .js files.
Don't deploy typescript files, that's going to add a lot of code to your remote code base.
The Error
this errors means some ts rules are not getting applied to your local ts.
I noticed rootDir is ./app and you have include as /app. Maybe it's looking for an /app folder under /app. Try ./ as root and see if that fixes it. Btw, I'm the creator of OvernightJS and I highly recommend you transpile all code before deploying.

Related

error TS2307: Cannot find module or its corresponding type declarations. on Github Actions

I can't find a way to fix this. It is working locally as usual. I have tried using path aliases, relative paths, updating the yaml file many times, but can't find it to work.
The gihub actions spits this
Run yarn build yarn run v1.22.19 warning package.json: No license field $ tsc Error: src/application/Feed/Activity/AddActivity.handler.ts(7,37): error TS2307: Cannot find module '#domain/Feed/Activity/Dto/Activity.response.dto' or its corresponding type declarations. Error: src/domain/Feed/Activity/Activity.mapper.ts(2,37): error TS2307: Cannot find module './Dto/Activity.response.dto' or its corresponding type declarations. Error: src/infrastructure/Serverless/Controller/Feed/Activity/AddActivity.controller.ts(4,37): error TS2307: Cannot find module '#domain/Feed/Activity/Dto/Activity.response.dto' or its corresponding type declarations. Error: src/infrastructure/Serverless/GetStream/Feed/Activity/Mapper/Activity.mapper.ts(6,37): error TS2307: Cannot find module '#domain/Feed/Activity/Dto/Activity.response.dto' or its corresponding type declarations. error Command failed with exit code 2. info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command. Error: Process completed with exit code 2.
this is my config
name: APP_NAME
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
workflow_dispatch:
jobs:
build_test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v3
with:
persist-credentials: false
- uses: actions/setup-node#v3
with:
node-version: lts/*
cache: yarn
- name: install
run: yarn --immutable
- name: build
run: yarn build
- name: Install B4A Cli
run: curl https://raw.githubusercontent.com/back4app/parse-cli/back4app/installer.sh | /bin/bash
- name: Configure B4A Cli and set it default
run: echo $ACCOUNT_KEY | b4a configure accountkey && b4a default $APP_NAME
- name: Deploy
run: b4a deploy
Package.json
{
"description": "",
"main": "src/main.js",
"scripts": {
"lint": "eslint src/",
"test": "jest",
"build": "tsc",
"dev:parse": "mongodb-runner start && parse-server --appId 'APP' --masterKey 'MASTER' --clientKey 'CLIENT' --databaseURI mongodb://localhost/test --cloud ./cloud/main.js",
"debug:parse": "mongodb-runner start && parse-server --appId 'APP' --masterKey 'MASTER' --clientKey 'CLIENT' --databaseURI mongodb://localhost/test --cloud ./cloud/main.js --verbose",
"dev:dash": "parse-dashboard --dev --appId APPLICATION_ID --masterKey MASTER_KEY --serverURL http://localhost:1337/parse --appName MY_APP"
},
"devDependencies": {
"#types/jest": "^29.2.3",
"#types/node": "^18.11.9",
"#types/node-fetch": "^2.6.2",
"#types/parse": "^3.0.1",
"#types/uuid": "^8.3.4",
"#typescript-eslint/eslint-plugin": "^5.42.1",
"#typescript-eslint/parser": "^5.42.1",
"eslint": "^8.27.0",
"jest": "^29.3.1",
"ts-jest": "^29.0.3",
"ts-node": "^10.9.1",
"typescript": "^4.8.4"
},
"dependencies": {
"class-transformer": "^0.5.1",
"class-validator": "^0.13.2",
"getstream": "^8.1.0",
"module-alias": "^2.2.2",
"parse-server": "^5.3.3",
"reflect-metadata": "^0.1.13",
"tsyringe": "^4.7.0",
"uuid": "^9.0.0"
},
"_moduleAliases": {
"#root": "cloud/",
"#domain": "cloud/domain",
"#application": "cloud/application",
"#infrastructure": "cloud/infrastructure"
}
}
tsconfig.json
{
"compilerOptions": {
"lib": ["es6"],
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"target": "ES2022",
"module": "commonjs",
"moduleResolution": "node",
"rootDir": "src/",
"baseUrl": ".",
"paths": {
"#root/*": ["./src/*"],
"#domain/*": ["./src/domain/*"],
"#application/*": ["./src/application/*"],
"#infrastructure/*": ["./src/infrastructure/*"],
},
"outDir": "cloud",
"removeComments": true,
"isolatedModules": true,
"allowSyntheticDefaultImports": true,
"esModuleInterop": false,
"forceConsistentCasingInFileNames": true,
"strict": true,
"strictPropertyInitialization": false,
"alwaysStrict": true,
"skipLibCheck": true
},
"include": ["src/"],
"exclude": ["src/**/*.spec.ts", "cloud", "node_modules"]
}
I have tried changing the github actions yaml config, switching from relative paths to path aliases, but can't make it work.
Delete node_modules folder and run npm install. Hope this will work

How to import d3.js in a node and typescript project? [ERR_REQUIRE_ESM]

I'm working on a node project using typescript, oclif and d3.js, among other things. Typescript was configured by npx when creating the project with oclif so I didn't control all aspects of it (especially regarding TS, and I'm not an expert. TS is great, but configuration wise, meh).
Anyway, everything was working fine, but I recently added d3 as a dependency and tried to build a document, and I get this error when running it:
(node:22554) [ERR_REQUIRE_ESM] Error Plugin: dbacl [ERR_REQUIRE_ESM]: require() of ES Module /dbacl/node_modules/d3/src/index.js from /dbacl/src/tree/graph-tree.ts not supported.
Instead change the require of index.js in /Users/nico/Furo/Dev/dbacl/src/tree/graph-tree.ts to a dynamic import() which is available in all CommonJS modules.
In the file where I'm using d3 I import it as per documentation:
import * as d3 from 'd3'
I suspect it might be a problem after compiling, so here is tsconfig.json. I didn't change anything since generation by npx.
{
"compilerOptions": {
"declaration": true,
"importHelpers": true,
"module": "commonjs",
"outDir": "dist",
"rootDir": "src",
"strict": true,
"target": "es2019",
},
"include": [
"src/**/*"
]
}
And in case it's more relevant, here is my package.json file as well (didn't change anything either):
{
"name": "dbacl",
"version": "0.0.0",
"description": "",
"author": "",
"bin": {
"dbacl": "./bin/run"
},
"license": "MIT",
"main": "dist/index.js",
"repository": "*****/dbacl",
"files": [
"/bin",
"/dist",
"/npm-shrinkwrap.json",
"/oclif.manifest.json"
],
"dependencies": {
"#oclif/core": "^1",
"#oclif/plugin-help": "^5",
"#oclif/plugin-plugins": "^2.0.1",
"#types/d3": "^7.4.0",
"#types/jsdom": "^16.2.14",
"d3": "^7.6.1",
"directory-tree": "^3.3.0",
"jsdom": "^20.0.0",
"lodash": "^4.17.21"
},
"devDependencies": {
"#oclif/test": "^2",
"#types/chai": "^4",
"#types/mocha": "^9.0.0",
"#types/node": "^16.9.4",
"chai": "^4",
"eslint": "^7.32.0",
"eslint-config-oclif": "^4",
"eslint-config-oclif-typescript": "^1.0.2",
"globby": "^11",
"mocha": "^9",
"oclif": "^3",
"shx": "^0.3.3",
"ts-node": "^10.2.1",
"tslib": "^2.3.1",
"typescript": "^4.4.3"
},
"oclif": {
"bin": "dbacl",
"dirname": "dbacl",
"commands": "./dist/commands",
"plugins": [
"#oclif/plugin-help",
"#oclif/plugin-plugins"
],
"topicSeparator": " ",
"topics": {
"hello": {
"description": "Say hello to the world and others"
}
}
},
"scripts": {
"build": "shx rm -rf dist && tsc -b",
"lint": "eslint . --ext .ts --config .eslintrc",
"postpack": "shx rm -f oclif.manifest.json",
"posttest": "yarn lint",
"prepack": "yarn build && oclif manifest && oclif readme",
"test": "mocha --forbid-only \"test/**/*.test.ts\"",
"version": "oclif readme && git add README.md"
},
"engines": {
"node": ">=12.0.0"
},
"keywords": [
"oclif"
],
"types": "dist/index.d.ts"
}
I find this problem quite confusing because everywhere I've been searching about this problem, it seems I'm doing the right thing. I assume the problem might be between my screen and my chair, but really I don't get it. Also, should I use the dynamic import like this?
const d3 = await import('d3')
I tried, it didn't work. I made the function that generates the document async and added that line at the start but nope. Anyway, if I believe the error, that would be the way to go but I didn't see this solution anywhere when looking for a solution to my problem. What do you guys think?
Thanks
So thanks to the input from a colleague and this article, the problem is that d3 now supports ESM only. So when the compiler turns the import into
// index.ts
import * as d3 from 'd3'
// compiled index.js
const d3 = require('d3')
it just doesn't work. I was using d3 version 7, and a quick fix to it was to downgrade to version 6.7.0, when it did support commonJS.
Now, I have to move on with this project, but I'll keep looking into it and into how I can use a recent version of d3 in my project. If I do, I'll edit that answer!
Cheers

Must use import to load ES module: ...\node_modules\got\dist\source\index.js

There's dozens of other questions with basically the same title, but none of the answers seem to be relevant and just add to the confusion.
This is my tsconfig.json:
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"lib": ["es2017", "es7", "es6", "dom"],
"declaration": true,
"outDir": "dist",
"strict": true,
"esModuleInterop": true
},
"exclude": [
"node_modules",
"dist"
]
}
This is what I typed:
import md5 from 'js-md5';
import got from 'got';
import { Design } from './Design';
...
This is my error:
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: C:\Users\...\node_modules\got\dist\source\index.js
require() of ES modules is not supported.
What. I'm not using require, I'm using import. I haven't had this error on any other modules, so why is Got different (and why is nothing ever simple)?
Why is this happening and how do I fix it?
As per #jsejcksn's answer, I tried changing my tsconfig.json to the following:
{
"compilerOptions": {
"target": "es6",
"module": "esnext",
"lib": ["es2017", "es7", "es6", "dom"],
"declaration": true,
"outDir": "dist",
"strict": true,
"esModuleInterop": true
},
"exclude": [
"node_modules",
"dist"
]
}
... and added "type": "module" to my package.json:
{
"dependencies": {
"body-parser": "^1.19.0",
"express": "^4.17.1",
"fs-extra": "^10.0.0",
"got": "^12.0.1",
"js-md5": "^0.7.3",
"moment": "^2.29.1",
"semver": "^7.3.5",
"typescript": "^4.4.3",
"uuid": "^8.3.2",
"#types/node": "^16.9.2",
"#types/express": "^4.17.13",
"#types/fs-extra": "^9.0.13",
"#types/semver": "^7.3.9",
"#types/uuid": "^8.3.1",
"#types/js-md5": "^0.4.3"
},
"type": "module"
}
But now I just get different errors :|
src/Logging.ts:1:20 - error TS2792: Cannot find module 'moment'. Did you mean to set the 'moduleResolution' option to 'node', or to add aliases to the 'paths' option?
1 import moment from 'moment';
src/SyncAPI.ts:2:17 - error TS2792: Cannot find module 'got'. Did you mean to set the 'moduleResolution' option to 'node', or to add aliases to the 'paths' option?
2 import got from 'got';
You have configured TypeScript to compile your modules to CommonJS format (your TSConfig option compilerOptions.module is set to "commonjs"). You'll need to set that option to an ESM-compatible setting (such as "esnext") so that the compiled JavaScript emitted by the TS compiler is in ESM format.
In your package.json, you'll probably also need to set the "type" field to "module".
Edit: To use npm packages by name as bare specifiers in your import statements, you'll also need to update your TSConfig compilerOptions.moduleResolution to "node". In your current config, you have included "dom" in the compilerOptions.lib array. "dom" is for environments with a DOM (which Node does not natively have), so that shouldn't be there. Instead, setting that array to ["es2017"] will include the others you have specified.
Edit: In attempt to provide more direct guidance:
Make sure you're not using an outdated version of Node (the current LTS is ^16.x.x)
Make sure you're not using an outdated version of TypeScript (currently at ^4.5.x)
Move all your source files to ./src (if they're not already in that directory), and
Use this as your ./tsconfig.json:
{
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"declaration": true,
"esModuleInterop": true,
"lib": [
"es2021"
],
"module": "esnext",
"moduleResolution": "node",
"outDir": "dist",
"strict": true,
"target": "es2021"
},
"include": [
"src/**/*"
]
}
and make sure that the "type" field in your ./package.json is set to "module":
{
// ...
"type": "module"
// ...
}
You shouldn't have any trouble with import statement module system compatibility after that.
In my case this very error was caused by ts-node-dev package.
If you use the same package (it usually sits in dev dependencies in package.json) you may check the resolution steps I posted in a parallel thread. Hope this helps.
You can also use tsc-node-esm instead of tsc-node to run your script after adding "type": "module" to your package.json

docker container build failing

I'm trying to move my service into a Docker container. Building the project works fine locally, but when I try building it as part of docker build, I get the following error:
node_modules/jest-extended/types/index.d.ts(135,39): error TS2694: Namespace 'jest' has no exported member 'Mock'.
Please find my dockerfile:
FROM node:12
COPY package.json /opt/service/
WORKDIR /opt/service/
RUN npm install
COPY . .
RUN npm run build
...
package.json parts that are relevant
"scripts": {
"build": "rm -rf lib && graphql-codegen && tsc -p tsconfig.json && copyfiles -u 1 ./src/**/*.graphql lib && copyfiles -u 1 ./src/**/*.proto lib"
},
"devDependencies": {
"#graphql-codegen/cli": "1.17.8",
"#graphql-codegen/typescript": "1.17.8",
"#graphql-codegen/typescript-resolvers": "^1.18.1",
"#types/jest": "^27.0.2",
"#types/node": "^16.10.2",
"copyfiles": "^2.3.0",
"graphql": "14.7.0",
"jest": "27.2.4",
"jest-cli": "27.2.4",
"jest-extended": "^0.11.5",
"nodemon": "^2.0.4",
"ts-jest": "^27.0.5",
"ts-node": "^10.2.1",
"typescript": "^3.9.7"
},
tsconfig.json
"compilerOptions": {
"target": "es2016",
"module": "commonjs",
"lib": ["es2019", "dom"],
"strict": true,
"noImplicitAny": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"typeRoots": ["node_modules/#types"],
"esModuleInterop": true,
"resolveJsonModule": true,
"outDir": "lib",
"rootDir": "src",
"resolveJsonModule": true,
"allowJs": true
},
"ts-node": {
"transpileOnly": true,
"files": true
},
"include": ["**/*"],
"exclude": ["node_modules", "lib", "jest.*.ts", "jest.*.js", "**/generated/*"]
Edit:
Adding "skipLibCheck": true in my compilerOptions fixed the Namespace 'jest' has no exported member 'Mock' error.
I'm still getting errors though that I didn't mention before that I hoped would get resolved by fixing the first error.
The Errors I'm still getting from *.test.ts files are:
error TS2708: Cannot use namespace 'jest' as a value.
error TS2304: Cannot find name 'expect'.
error TS2304: Cannot find name 'beforeAll'.
error TS2582: Cannot find name 'test'.
I have an idea that maybe could resolve your issue, as I had a similar problem that was caused because of version conflicts. Could you try to also copy your package-lock.json file to your docker image?
...
COPY package.json /opt/service/
COPY package-lock.json /opt/service/
...
This turned out to be a problem with tsconfig.json and my folder structure. I have multiple services and packages in this project. In the root folder (two folders up from each service) I have a tsconfig.json file that other services and packages are extending. Because I'm only copying the tsconfig.json file from the root and not node_modules, and the root tsconfig.json file is declaring "typeRoots": ["node_modules/#types"], services expect #types to be found in ../../node_modules.

TypeScript: Could not find a declaration file for module in unit tests, only

I'm using TypeScript with Visual Studio Code on Windows 10 to develop an NPM module. I use mocha/chai combined with nyc (istanbul) for unit testing and code coverage.
For some of my tests I would like to use chai-bytes to compare buffers more easily. Unfortunately, there is no type definition file in the chai-bytes module and there is no definition at #types/chai-bytes available.
Therefore, I have written my own type definition file for the chai-bytes plugin (which is very simple), but during execution of npm test I get the following error:
TSError: тип Unable to compile TypeScript:
test/utls/BitArray-test.ts(3,23): error TS7016: Could not find a declaration file for module 'chai-bytes'. 'C:/Users/<user>/Source/Repos/velux-api/node_modules/chai-bytes/index.js' implicitly has an 'any' type.
Try `npm install #types/chai-bytes` if it exists or add a new declaration (.d.ts) file containing `declare module 'chai-bytes';`
test/utls/BitArray-test.ts(48,38): error TS2339: Property 'equalBytes' does not exist on type 'Assertion'.
VS Code provides me with full Intellisense, so I think my type definition file works and is found at least by VS Code.
This is my directory structure:
dist\ <-- My compiled code goes here
utils\
BitArray.d.ts
BitArray.js
BitArray.js.map
index.d.ts
index.js
index.js.map
...
src\
utils\
BitArray.ts
index.ts
...
test\
utils\
BitArray-test.ts
... (other test files)
mocha.opts
types\
chai-bytes\
index.d.ts <-- Type definition file for 'chai-bytes'
I have tried to move the type definition file to the source tree (several places), but with no effect, besides, that sometimes it got even worse, so that even VS Code haven't found it anymore.
These are my config files:
package.json:
{
"name": "klf-200-api",
"version": "3.0.0",
"description": "This module provides a wrapper to the socket API of a Velux KLF-200 interface. You will need at least firmware 0.2.0.0.71 on your KLF interface for this library to work.",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"author": {
"name": "Michael Schroeder"
},
"dependencies": {
"#types/promise-timeout": "^1.3.0",
"promise-timeout": "^1.3.0"
},
"devDependencies": {
"#types/chai": "^4.1.6",
"#types/chai-as-promised": "^7.1.0",
"#types/mitm": "^1.3.2",
"#types/mocha": "^5.2.5",
"#types/node": "^10.11.7",
"#types/sinon": "^5.0.7",
"#types/sleep": "0.0.7",
"babel-eslint": "^8.0.0",
"chai": "^4.1.0",
"chai-as-promised": "^7.1.1",
"chai-bytes": "^0.1.1",
"chai-sinon": "^2.8.1",
"cross-env": "^5.2.0",
"eslint": "^4.7.1",
"eslint-config-defaults": "^9.0.0",
"eslint-plugin-react": "^7.3.0",
"gulp": "^4.0.0",
"gulp-release-it": "^2.0.14",
"gulp-typescript": "^5.0.0-alpha.3",
"gulp-uglify": "^3.0.1",
"istanbul": "^0.4.5",
"mitm": "^1.4.0",
"mocha": "^3.4.2",
"nock": "^9.0.14",
"nyc": "^13.1.0",
"sinon": "^7.1.1",
"sleep": "^5.2.3",
"source-map-support": "^0.5.9",
"ts-mocha": "^2.0.0",
"ts-node": "^7.0.1",
"typescript": "^3.1.2",
"uglify-es": "^3.3.9"
},
"scripts": {
"test": "cross-env TS_NODE_FILES=true nyc mocha",
"document": "jsdoc src -r -c ./.jsdoc.json -d docs"
},
"nyc": {
"include": [
"src/**/*.ts",
"src/**/*.tsx"
],
"extension": [
".ts",
".tsx"
],
"exclude": [
"**/*.d.ts"
],
"reporter": [
"text-summary",
"html"
],
"all": true
},
"repository": {
"type": "git",
"url": "https://github.com/MiSchroe/klf-200-api"
},
"keywords": [
"klf-200",
"IoT"
],
"license": "MIT",
"bugs": {
"url": "https://github.com/MiSchroe/klf-200-api/issues"
},
"homepage": "https://mischroe.github.io/klf-200-api/"
}
tsconfig.json:
{
"compilerOptions": {
/* Basic Options */
"target": "es6", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
"declaration": true, /* Generates corresponding '.d.ts' file. */
"sourceMap": true, /* Generates corresponding '.map' file. */
"outDir": "./dist", /* Redirect output structure to the directory. */
/* Strict Type-Checking Options */
"strict": true, /* Enable all strict type-checking options. */
"noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
/* Module Resolution Options */
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
},
"include": [
"./src/**/*"
]
}
mocha.opts:
--require ts-node/register
--require source-map-support/register
--recursive
--full-trace
--bail
test/**/*.ts
types\chai-bytes\index.d.ts:
/// <reference types="chai" />
declare module "chai-bytes" {
function chaiBytes(chai: any, utils: any): void;
export = chaiBytes;
}
declare namespace Chai {
// For BDD API
interface Assertion extends LanguageChains, NumericComparison, TypeComparison {
equalBytes(expected: string | Array<number> | ArrayLike<number>): void;
}
}
BitArray-test.ts (only the relevant test):
import { expect } from "chai";
import { bitArrayToArray, arrayToBitArray } from "../../src/utils/BitArray";
import chaibytes from "chai-bytes";
'use strict';
chai.use(chaibytes);
describe("...", function() {
it("should return an the correctly filled buffer", function() {
const nums: number[] = [0, 2, 4, 6, 8, 10, 12, 14];
const result = arrayToBitArray(nums, 2);
expect(result).to.be.an.instanceof(Buffer);
expect(result).to.be.equalBytes([0x55, 0x55]);
});
});
npm --version:
3.10.10
node --version:
v6.11.1
I could use Buffer.compare as work-around instead, but then I wouldn't see the buffers' content in the error message but only a -1, 0 or 1. (And it wouldn't solve the problem.)
Currently, I'm stuck at that point and any help is much appreciated.
At tsconfig.json add:
"typeRoots": [
"./node_modules/#types",
"./types"
] /* List of folders to include type definitions from. */
to the compilerOptions list.
Change the header of the BitArray-test.ts file:
import { bitArrayToArray, arrayToBitArray } from "../../src/utils/BitArray";
import chaibytes from "chai-bytes";
import { expect, use } from "chai";
'use strict';
use(chaibytes);

Resources