Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to ignore type incompatibility in typescript?

Tags:

typescript

I have been looking at using typescript with mongoose for MongoDB. Mostly it has been working great, but with certain types of quesites I get warnings from the typescript compiler.

If I do an or like so:

{"$or": [{done: {"$exists": false}}, {done:false}]} 

I get the following warning:

Incompatible types in array literal expression: Types of property 'done' of types '{ done: { $exists: bool; }; }' and '{ done: bool; }' are incompatible.

I understand why, but is there a way to express this so the compiler will accept it?

like image 343
Christian Nielsen Avatar asked Feb 06 '13 19:02

Christian Nielsen


People also ask

How do I ignore type errors in TypeScript?

You can ignore type checking errors for a single line by using the // @ts-ignore comment. Copied! The // @ts-ignore comment ignores any type checking errors that occur on the next line. If you use a linter, you might get an error when you use a comment to disable type checking for a line, or for the entire file.

How do I ts ignore a file?

Use the // @ts-nocheck comment to disable type checking for an entire file in TypeScript. The // @ts-nocheck comment instructs TypeScript to skip the file when type checking.

What is Typings in TypeScript?

Typings is the simple way to manage and install TypeScript definitions. It uses typings. json , which can resolve to the Typings Registry, GitHub, NPM, Bower, HTTP and local files. Packages can use type definitions from various sources and different versions, knowing they will never conflict for users.

Does TypeScript enforce types?

Use the Record utility type to enforce the type of an object's values in TypeScript, e.g. type Animal = Record<string, string> . The Record utility type constructs an object type, whose keys and values are of specific type.


1 Answers

You can type-assert any of the elements to any to "turn off" type checking:

[<any>{done: {"$exists": false}}, {done:false}]

Or, if you're initializing a variable, you can do something like this:

var n: any[] = [{done: {"$exists": false}}, {done:false}]
like image 153
Ryan Cavanaugh Avatar answered Sep 19 '22 19:09

Ryan Cavanaugh