ESLint: constructors and enum values are reported as unused but they are used in app.ts - javascript

I have a NodeJS project with Express and TypeScript.
Some of the classes/enums usied in the project are reported by ESLint as unused althought they are used in the project's entrypoint file (app.ts in my case).
For instance, I have a class defined in handler/RequestHandler.ts as follows:
export class RequestHandler {
constructor (private readonly manager: Manager) {}
...
}
The in the app.ts it is used like this:
import { RequestHandler } from './handler/RequestHandler'
//....
const handler = new RequestHandler(manager)
But when I run linter I get:
/path-to-my-project-root/src/handler/RequestHandler.ts
5:2 error Useless constructor no-useless-constructor
I thought it ignores app.ts but if I break linter rules for app.ts (for instance by adding unneeded spaces) it would report it.
The same happens with enumeration values.
Question: How to make ESLint see constructor/enum usages in the app.ts file?

Check out the project FAQ which answers your question:
https://typescript-eslint.io/docs/linting/troubleshooting#i-am-using-a-rule-from-eslint-core-and-it-doesnt-work-correctly-with-typescript-code
Specifically, you're using the wrong lint rule.
https://typescript-eslint.io/rules/no-useless-constructor/

Related

Import JS web assembly into TypeScript

I'm trying to use wasm-clingo in my TypeScript React project. I tried to write my own d.ts file for the project:
// wasm-clingo.d.ts
declare module 'wasm-clingo' {
export const Module: any;
}
and import like this:
import { Module } from 'wasm-clingo';
but when I console.log(Module) it says undefined. What did I do wrong?
Notes:
clingo.js is the main js file.
index.html and index_amd.html are two example pages
Solution:
I solved the problem like this:
// wasm-clingo.d.ts
declare module 'wasm-clingo' {
const Clingo: (Module: any) => Promise<any>;
namespace Clingo {}
export = Clingo;
}
and
import * as Clingo from 'wasm-clingo';
Here's the source for this solution
I know you found a solution acceptable to you; however, you don't really have any types here, you just have Module declared as any, which gives you no typescript benefits at all. In a similar situation I used #types/emscripten, which provides full type definitions for web assembly modules compiled using emscripten. You simply need to do:
npm install --save-dev #types/emscripten
then change your tsconfig.json types array to add an entry for emscripten.
After that you can just write Module.ccall(...) etc. If you like you could of course write const Clingo = Module and then make calls against that if you want a more descriptive name than Module (which is a terrible name!).
You're welcome ;)
I think the issue is that wasm-clingo exports the module itself but import { Module } from 'wasm-clingo' expects a property.
Try
import Clingo_ from 'wasm-clingo';
const Clingo: typeof Clingo_ = (Clingo_ as any).default || Clingo_;

Flow type issue with inline webpack loader

I am using sass-variable-loader with webpack and because of limitation with extending webpack configuration, I just want to use it inline this way:
import variables from '!!sass-variable-loader!../../../src/styles/variables.scss'
This works as expected but when I try to run flow, it exits with this error:
import variables from '!!sass-variable-loader!../../../src/styles/variables.scss'
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ !!sass-variable-loader!../../../src/styles/variables.scss. Required module not found
Is there a way for Flow not to fail on this line?
In your .flowconfig file, you can add the following (I have the initial !s optional, since they are, depending on your purpose):
[options]
module.name_mapper='^\(!!\)?sass-variable-loader!.+\.scss$' -> '<PROJECT_ROOT>/Stub.js.flow'
And Stub.js.flow looks like:
declare module Stub {
declare var exports: { [key: string]: string };
}
You don't need to call the file that, but you'll need one that looks like that.

Typescript 2: Custom Type definition in node_modules

Let's say I publish a custom library called my-library. This library will be of the structure
node_modules/
my-library/
lib/
index.js
src/
...
typings/
index.d.ts
package.json
Say now I export default something called Application from the index.js, so you use it by import Application from 'my-library';
Now, in my index.d.ts I want to declare its typing:
export = Application;
export as namespace Application;
declare namespace Application {
function foo(arg1: string): object;
}
I know expect this to return an error but it does not:
import Application from 'my-library';
Application.foo(123)[0].blahblah;
Not only is the argument wrong, but so is the use of the return value. I don't know how I am supposed to add my type definitions so that the above works.
You can look here to see where Typescript looks for definitions.
What you probably want is to add a types in your package.json to point to a custom definition file.
{
"types": "typings/index.d.ts",
}

How do we declare import types from NPM library that has no declaration files?

For example, if I have the following in my app,
import Node from 'infamous/motor/Node'
console.log(Node)
that works just fine. But the moment I actually do something with it,
import Node from 'infamous/motor/Node'
console.log(new Node)
then TypeScript will complain because there's no type definition for Node. How do I define the type of Node?
The library has no type declarations of it's own. I tried something like
import MotorNode from 'infamous/motor/Node'
declare class MotorNode {}
console.log(' --- ', new MotorNode)
but I get the error
./src/app.tsx(6,8): error TS2440: Import declaration conflicts with local declaration of 'MotorNode'
When I need to do what you are trying to do, I create an externals.d.ts file in which I put module augmentations for my project and make sure that my tsconfig.json includes it in the compilation.
In your case the augmentation might look something like this:
declare module "infamous/motor/Node" {
class Node {
// Whatever you need here...
}
export default Node;
}
I put it in a separate file because a module augmentation like this has to be global (must be outside any module), and a file that contains a a top-level import or export is a module. (See this comment from a TypeScript contributor.)

How to import a js library without definition file in typescript file

I want to switch from JavaScript to TypeScript to help with code management as our project gets larger. We utilize, however, lots of libraries as amd Modules, which we do not want to convert to TypeScript.
We still want to import them into TypeScript files, but we also do not want to generate definition files. How can we achieve that?
e.g. The new Typescript file:
/// <reference path="../../../../definetelyTyped/jquery.d.ts" />
/// <reference path="../../../../definetelyTyped/require.d.ts" />
import $ = require('jquery');
import alert = require('lib/errorInfoHandler');
Here, lib/errorInfoHandler is an amd module included in a huge JavaScript library that we do not want to touch.
Using the above code produces the following errors:
Unable to resolve external module ''lib/errorInfoHandler''
Module cannot be aliased to a non-module type.
This should actually produce the following code:
define(["require", "exports", "jquery", "lib/errorInfoHandler"], function(require, exports, $, alert) {
...
}
Is there a way to import a JavaScript library into TypeScript as an amd Module and use it inside the TypeScript file without making a definition file?
A combination of the 2 answers given here worked for me.
//errorInfoHandler.d.ts
declare module "lib/errorInfoHandler" {
var noTypeInfoYet: any; // any var name here really
export = noTypeInfoYet;
}
I'm still new to TypeScript but it looks as if this is just a way to tell TypeScript to leave off by exporting a dummy variable with no type information on it.
EDIT
It has been noted in the comments for this answer that you can achieve the same result by simply declaring:
//errorInfoHandler.d.ts
declare module "*";
See the github comment here.
Either create your own definition file with following content:
declare module "lib/errorInfoHandler" {}
And reference this file where you want to use the import.
Or add the following line to the top of your file:
/// <amd-dependency path="lib/errorInfoHandler">
Note: I do not know if the latter still works, it's how I initially worked with missing AMD dependencies. Please also note that with this approach you will not have IntelliSense for that file.
Create a file in lib called errorInfoHandler.d.ts. There, write:
var noTypeInfoYet: any; // any var name here really
export = noTypeInfoYet;
Now the alert import will succeed and be of type any.
Typically if you just want to need a temporary-faster-solution, that could be done by defining a new index.d.ts in the root of the project folder, then make a module name like described inside package.json file
for example
// somefile.ts
import Foo from '#awesome/my-module'
// index.d.ts on #awesome/my-module
declare module '#awesome/my-module' {
const bind: any;
export default bind;
}
Ran into that that problem in 2020, and found an easy solution:
Create a decs.d.ts file in the root of your TS project.
Place this declaration:
declare module 'lib/errorInfoHandler';
This eliminates the error in my case. I'm using TypeScript 3.9.7

Categories

Resources