crcn / sift.js

Use Mongodb queries in JavaScript
MIT License
1.64k stars 100 forks source link

Tyepscript definition is broken #150

Closed marcj closed 5 years ago

marcj commented 5 years ago

Given following test

import sift from 'sift';

test('test sift', async () => {
    class File {
        constructor(public readonly path: string) {
        }
    }

    {
        const filtered = sift<File[]>({
            path: 'test1'
        }, [new File('test1'), new File('test2')]);

        expect(filtered.length).toBe(1);
    }

    {
        const filtered = sift<File[]>({
            path: /^test/        // <--- Typescript error
        }, [new File('test1'), new File('test2')]);

        expect(filtered.length).toBe(1);
    }

    {
        const filtered = sift<File[]>({
            path: {$regex: /^test/}       // <--- Typescript error
        }, [new File('test1'), new File('test2')]);

        expect(filtered.length).toBe(1);
    }
});

I get following Typescript errors:

Error:(26, 13) TS2322: Type 'RegExp' is not assignable to type 'SiftQuery<string>'.
  Type 'RegExp' is not assignable to type 'string'.
Error:(34, 13) TS2322: Type '{ $regex: RegExp; }' is not assignable to type 'SiftQuery<string>'.
  Type '{ $regex: RegExp; }' is not assignable to type 'string'.

I use typescript 3.3.3333

davidlav commented 5 years ago

Ditto. I just tried running the first example from the readme in TypeScript

import sift from 'sift';

const result = ["hello", "sifted", "array!"].filter(
  sift({ $in: ["hello", "world"] })
); 

and got

Argument of type '{ $in: string[]; }' is not assignable to parameter of type 'SiftQuery<any[][]>'.
  Type '{ $in: string[]; }' is not assignable to type 'ElemMatch<any[]>'.
    Types of property 'toString' are incompatible.
      Type '() => string' is not assignable to type 'SiftQuery<() => string>'.
        Type '() => string' is not assignable to type 'ElemMatch<any>'.
          Index signature is missing in type '() => string'.
crcn commented 5 years ago

Updated *d.ts file here: https://github.com/crcn/sift.js/blob/master/index.d.ts.

Still need to add more tests, but this issue should be fixed by next week. 🙂

davidlav commented 5 years ago

Fantastic, thanks!

JohnHandley commented 5 years ago

Please release v8.2.0 this next week lol, I appreciate the fix!

crcn commented 5 years ago

Just pushed v8.2.0 to NPM.