Typescript compiling errors using 'vss-web-extension-sdk' & 'azure-pipelines-task-lib' - node.js

I am trying to follow the following guide to make a custom task in azure devops https://learn.microsoft.com/en-us/azure/devops/extend/develop/add-build-task?view=azure-devops
When I install both 'azure-pipelines-task-lib' & 'vss-web-extension-sdk'
and have a single typescript file that requires the task-lib, like the guide suggests, I get tons of typescript errors.
package.json
"dependencies": {
"azure-pipelines-task-lib": "^3.1.9",
"typescript": "^4.4.3",
"vss-web-extension-sdk": "^5.141.0"
}
tsconfig.json
{
"compilerOptions": {
"module": "commonjs",
"moduleResolution": "node",
"target": "es6",
"rootDir": "src/",
"outDir": "dist/",
"types": [
"vss-web-extension-sdk"
]
},
"files": [
"src/index.ts"
]
}
index.ts
import tl = require('azure-pipelines-task-lib/task');
export function Foo() {
return "BAR"
}
File structure
Root
tsconfig.json
package.json
src
index.ts
dist
index.js
Errors
https://imgur.com/4aNJetK
node_modules/#types/node/module.d.ts:2:5 - error TS2300: Duplicate identifier 'mod'.
2 export = NodeJS.Module;
~~~~~~~~~~~~~~~~~~~~~~~
node_modules/#types/requirejs/index.d.ts:38:14
38 export = mod;
~~~
'mod' was also declared here.
node_modules/#types/requirejs/index.d.ts:38:14 - error TS2300: Duplicate identifier 'mod'.
38 export = mod;
~~~
node_modules/#types/node/module.d.ts:2:5
2 export = NodeJS.Module;
~~~~~~~~~~~~~~~~~~~~~~~
'mod' was also declared here.
node_modules/#types/requirejs/index.d.ts:422:13 - error TS2403: Subsequent variable declarations must have the same type. Variable 'require' must be of type
'NodeRequire', but here has type 'Require'.
422 declare var require: Require;
~~~~~~~
node_modules/#types/node/globals.d.ts:213:13
213 declare var require: NodeRequire;
~~~~~~~
'require' was also declared here.
node_modules/vss-web-extension-sdk/typings/vss.d.ts:3168:13 - error TS2403: Subsequent variable declarations must have the same type. Variable 'require' must be of type 'NodeRequire', but here has type 'Require'.
3168 declare var require: Require;
~~~~~~~
node_modules/#types/node/globals.d.ts:213:13
213 declare var require: NodeRequire;
~~~~~~~
'require' was also declared here.
Can someone provide me with ANY working example of compiling typescript when these libraries are installed?

Related

How do I prevent tsc from including browser types in the compliation

I have a script that uses ts-node:
#!/usr/binenv ts-node
const top: number[] = [];
but tsc complains:
top3.ts(3,7): error TS2451: Cannot redeclare block-scoped variable 'top'.
because apparently top is a global variable in browsers.
I've installed #types/node and my tsconfig.json reads:
{
"compilerOptions": {
"noImplicitAny": true,
"target": "es6",
"types": ["node"],
}
}
so I can refer to node builtins like process.
How do I configure tsc so that it does not include browser builtins, but only pure ECMAScript + node.js builtins?

exports is not defined when running compiled typescript

I am trying to take my first steps into working with typescript and I've run into an issue when trying to run my application.
I get the error ReferenceError: exports is not defined
the code I have is quite simple:
// --src/changeset.ts
export enum ChangeAction {
ADD,
DELETE,
MODIFY
}
export class Changeset {
constructor(
public version: Number,
public content: String,
public path: String,
public action: ChangeAction
) {}
}
// --src/index.ts
import { Changeset, ChangeAction } from "./changeset";
const set = new Changeset(0, "Hello world", "/dev/null", ChangeAction.ADD);
set.version = 0;
console.log("Hello World! " + set.version);
// --tsconfig.json
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"outDir": "build"
},
"include": ["src/**/*"]
}
running tsc, it compiles and seems to work without any real issues, however when I try to run it with node build/index.js it crashes with this
build/index.js:2
Object.defineProperty(exports, "__esModule", { value: true });
^
ReferenceError: exports is not defined
It feels like I am missing something quite obvious, but I can't really seem to put my finger on it, so what am I missing?
You appear to have enabled Node's ES modules by setting "type": "module" in your package.json, but your tsconfig tells typescript to emit code compatible with CommonJS.
Either remove "type": "module", or configure tsconfig to emit code targeting ES modules.

Starting nestjs in production mode

I am using akveo backend bundle that I bought, and while everything seems to be working fine in development mode starting in production gives me following errors, I am new to nestjs itself.
Anyone know what's going here?
node_modules/#nestjs/core/adapters/http-adapter.d.ts:5:31 - error TS2420: Class 'AbstractHttpAdapter<TServer, TRequest, TResponse>' incorrectly implements interface 'HttpServer<TRequest, TResponse>'.
Property 'status' is missing in type 'AbstractHttpAdapter<TServer, TRequest, TResponse>' but required in type 'HttpServer<TRequest, TResponse>'.
5 export declare abstract class AbstractHttpAdapter<TServer = any, TRequest = any, TResponse = any> implements HttpServer<TRequest, TResponse> {
~~~~~~~~~~~~~~~~~~~
node_modules/#nestjs/common/interfaces/http/http-server.interface.d.ts:26:5
26 status(response: any, statusCode: number): any;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'status' is declared here.
node_modules/#nestjs/core/application-config.d.ts:2:39 - error TS2307: Cannot find module '#nestjs/common/interfaces/configuration-provider.interface'.
2 import { ConfigurationProvider } from '#nestjs/common/interfaces/configuration-provider.interface';
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
node_modules/#nestjs/core/guards/guards-context-creator.d.ts:3:39 - error TS2307: Cannot find module '#nestjs/common/interfaces/configuration-provider.interface'.
3 import { ConfigurationProvider } from '#nestjs/common/interfaces/configuration-provider.interface';
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
node_modules/#nestjs/core/interceptors/interceptors-context-creator.d.ts:2:39 - error TS2307: Cannot find module '#nestjs/common/interfaces/configuration-provider.interface'.
2 import { ConfigurationProvider } from '#nestjs/common/interfaces/configuration-provider.interface';
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
node_modules/#nestjs/core/nest-application.d.ts:24:5 - error TS2416: Property 'getHttpAdapter' in type 'NestApplication' is not assignable to the same property in base type 'INestApplication'.
Type '() => AbstractHttpAdapter<any, any, any>' is not assignable to type '() => HttpServer<any, any>'.
Property 'status' is missing in type 'AbstractHttpAdapter<any, any, any>' but required in type 'HttpServer<any, any>'.
24 getHttpAdapter(): AbstractHttpAdapter;
~~~~~~~~~~~~~~
node_modules/#nestjs/common/interfaces/http/http-server.interface.d.ts:26:5
26 status(response: any, statusCode: number): any;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'status' is declared here.
Found 5 errors.
I am using tsc -p tsconfig.build.json command to build it with tsconfig.build.json:
{
"extends": "./tsconfig.json",
"exclude": ["node_modules", "test", "**/*spec.ts"]
}
and tsconfig.json:
{
"compilerOptions": {
"module": "commonjs",
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es6",
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./"
},
"exclude": ["node_modules"]
}
I expect that this config should compile typescript code to javascript which then I would run with node dist/main.js command. Which it actually does. But I am worried about the typescript compiler errors.
Not sure without the code but got similar mismatches once upon a time in nest core packages. Check out package.json and ensure that all #nest packages have the same version, as sometimes they have 'breaking' changes even if it wasn't major release.
For an abstract example, something like that gave me errors related to core when building production:
"#nestjs/common": "6.6.0", // notice the core/common does not match, it should be bumped
"#nestjs/core": "6.6.2",

TS2451: Cannot redeclare block-scoped variable 'custom'

I'm using typescript in a web project. I use awesome-typescript-loader as a webpack loader. I am getting error when building my project:
ERROR in [at-loader] ./node_modules/#types/node/index.d.ts:82:15
TS2451: Cannot redeclare block-scoped variable 'custom'.
ERROR in [at-loader] ./node_modules/#types/node/index.d.ts:85:15
TS2451: Cannot redeclare block-scoped variable 'custom'.
ERROR in [at-loader] ./node_modules/#types/node/ts3.2/util.d.ts:7:15
TS2451: Cannot redeclare block-scoped variable 'custom'.
ERROR in [at-loader] ./node_modules/#types/node/ts3.2/util.d.ts:10:15
TS2451: Cannot redeclare block-scoped variable 'custom'.
I initiated a complete new folder with just typescript and #types/typescript installed, I can still see the same error complained by visual studio code.
Versions below:
"dependencies": {
"#types/node": "^11.13.6",
"typescript": "^3.4.4"
}
As the error showed above, I found
in index.d.ts:
declare module "util" {
namespace inspect {
const custom: symbol;
}
namespace promisify {
const custom: symbol;
}
namespace types {
function isBigInt64Array(value: any): boolean;
function isBigUint64Array(value: any): boolean;
}
}
in util.d.ts:
declare module "util" {
namespace inspect {
const custom: unique symbol;
}
namespace promisify {
const custom: unique symbol;
}
namespace types {
function isBigInt64Array(value: any): value is BigInt64Array;
function isBigUint64Array(value: any): value is BigUint64Array;
}
}
We can see custom is indeed being re-declared in index.d.ts and util.d.ts.
So my question is how to fix this issue? Is this a bug of #types/node?
I was facing the same issue. Removing reference to node in tsconfig fixed the issue for me.
Sample tsconfig.
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"outDir": "dist",
"sourceMap": true
},
"files": [
// "./node_modules/#types/node/index.d.ts",
"./node_modules/#types/express/index.d.ts"
],
"include": [
"src/**/*.ts"
],
"exclude": [
"node_modules"
],
"lib": [
"es2017"
]
}
My problem is that I accidentally included node_modules/#types. Fixed by comment it out like this:
"include": ["src/**/*.ts","__tests__/**/*.ts"/*, "node_modules/#types"*/]
I had the same issue, but the error message was misleading just because of something wrong cached. I tried many things, but finally solved it by removing npm_modules, clearing cache, and new installation:
npm cache clean --force
npm install
I added this piece of code twice in protractor.conf.js file
require('ts-node').register({
project: require('path').join(__dirname, './tsconfig.e2e.json')
});

How to reference externally defined node modules with string identifiers with typescript

I'm writing a set of node helper modules in typescript. I'm having difficulty getting typescript to interpret type information for external node modules such as "fs" and "path".
Importantly, I want to separate my module into a bunch of Typescript files, with a class/interface per file. They file layout is like this:
ts/ISomeInterface1.ts
ts/ISomeInterface2.ts
ts/SomeClass1.ts
ts/SomeClass2.ts
A class instantiates one or more interfaces, and is written as follows:
///<reference path="IFileSystemHelpers.ts" />
var fs = require("fs");
namespace myNmspace {
export class SomeClass1 implements SomeInterface1 {
public someIFunction() {
//do work
}
}
}
I'm using gulp-typescript to install type declarations for NodeJs. And I use a tsconfig.json file to build and reference these external typings. Here's a snippet:
{
"version": "1.8.9",
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"noImplicitAny": false,
"removeComments": true,
"noLib": false,
"preserveConstEnums": true,
"declaration": true,
"suppressImplicitAnyIndexErrors": true,
"out": "./outputfile.js"
},
"filesGlob": [
"./**/*.ts",
"!./node_modules/**/*.ts"
],
"files": [
"./typings/main.d.ts",
"./ts/ISomeInterface1.ts",
"./ts/ISomeInterface2.ts",
"./ts/SomeClass1.ts",
"./ts/SomeClass2.ts",
"./ts/exports.ts"
]
}
Then classes are exported in the exports.ts file:
declare var exports: any;
if (exports) {
exports.SomeClass1 = myNmspace.SomeClass1;
exports.SomeClass2 = myNmspace.SomeClass2;
}
Then comes my problem. How do I get type information for the "fs" module?
I can see the following in the node.d.ts file (see here)that typings has installed:
declare module "fs" {
import * as stream from "stream";
import * as events from "events";
...
}
How do I force Typescript to interpret my fs variable in the SomeClass1.ts file as strongly-typed? In other words, what do I write here:
var fs : ??? = require("fs");
Can anyone help?
As an aside, I've noticed that if I replace the var with an import keyword, I get correct type interpretation for the fs variable. However the terms which point to my interfaces break and I get a squiggly line under the implements ISomeInterface1. Changing the pattern to use imports breaks my file separation, and seems valid only if I want to create a single-file node module.
Use ES6 style imports
import * as fs from 'fs'
import * as path from 'path'
This will also import the definitions from the definition file.
The syntax var x = require('x') does not (however import x = require('x') does, to add to the confusion)

Resources