Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignore "cannot find module" error on typescript

Can the Typescript compiler ignore the cannot find module 'x' error on import expressions such as:

//How to tell the compiler that this module does exists import sql = require('sql'); 

There are multiple npm libraries such as node sql that doesn't have existing typings

Is there a way to tell the compiler to ignore this error other than creating a new definition file with the declare module x ... ?

like image 384
Rafael Avatar asked May 20 '16 20:05

Rafael


People also ask

How do I ignore TypeScript error?

Use // @ts-ignore to ignore the type checking errors on the next line in a TypeScript file. If you use a linter, you might have to add a comment to also suppress linting errors when using ts-ignore - // eslint-disable-next-line @typescript-eslint/ban-ts-comment .

How do you solve Cannot find module or its corresponding type declarations?

The "Cannot find module or its corresponding type declarations" error occurs when TypeScript cannot locate a third-party or local module in our project. To solve the error, make sure to install the module and try setting moduleResolution to node in your tsconfig. json file.

What is declare module in TypeScript?

The TypeScript declares module is one of the modules and keyword it is used for to surround and define the classes, interfaces; variables are also declared it will not originate with the TypeScript like that module is the set of files that contains values, classes, functions/methods, keywords, enum all these contains ...

Can not find module angular error?

To solve the error "Cannot find module '@angular/core'", make sure you have installed all dependencies by running the npm install command, set the baseUrl option to src in your tsconfig. json file and restart your IDE and development server. Copied!

How to solve the cannot find module 'typescript' error?

To solve the cannot find module 'typescript' error, make sure to install typescript globally by running the npm i -g typescript command and create a symbolic link from the globally-installed package to node_modules by running the npm link typescript command. Open your terminal in your project's root directory and run the following commands:

How to solve the “cannot find module or its corresponding type declarations” error?

The "Cannot find module or its corresponding type declarations" error occurs when TypeScript cannot locate a third-party or local module in our project. To solve the error, make sure to install the module and try setting moduleResolution to node in your tsconfig.json file. If the module is a third-party module, make sure you have it installed.

Why does the “cannot find module” error happen when loading fonts?

# Why Does the "Cannot find module" Error Happen When Loading Fonts? It happens because of module resolution. You need to declare the font file formats as modules so that TypeScript can understand and parse them correctly. TypeScript relies on definition files (*.d.ts) to figure out what an import refers to.

How to declare fonts as modules in typescript?

If you're trying to import fonts into your typescript project and getting the following error: Then you probably need to declare the font file type (s) as modules so TypeScript can recognize them for import. How to Declare Files as Modules in TypeScript? Create a .d.ts file (for e.g. fonts.d.ts) and add your file definitions in there. For example:


2 Answers

If you just want to bypass the compiler, you can create a .d.ts file for that module, for instance, you could create a sql.d.ts file and inside have this:

declare module "sql" {   let _sql: any;   export = _sql; } 
like image 26
thitemple Avatar answered Sep 27 '22 21:09

thitemple


As of TypeScript 2.6 (released on Oct 31, 2017), now there is a way to ignore all errors from a specific line using // @ts-ignore comments before the target line.

The mendtioned documentation is succinct enough, but to recap:

// @ts-ignore const s : string = false 

disables error reporting for this line.

However, this should only be used as a last resort when fixing the error or using hacks like (x as any) is much more trouble than losing all type checking for a line.

As for specifying certain errors, the current (mid-2018) state is discussed here, in Design Meeting Notes (2/16/2018) and further comments, which is basically

"no conclusion yet"

and strong opposition to introducing this fine tuning.

like image 199
stsloth Avatar answered Sep 27 '22 21:09

stsloth