-
Notifications
You must be signed in to change notification settings - Fork 99
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Generic types? #197
Comments
This was a bit tricky, but it's finally done. This update has been added to |
Thank you! I'll test as soon as I can and ping you here :) |
@crcn unfortunately this passes TS check (even though it should not): const objects = [{ string: 'hello', number: 1 }].filter(sift({
string: 1234,
number: 'world',
})); I do get the variable names & types in autocomplete, but when I'm providing an invalid value (wrong type) then TS does not show me any errors (types are transformed into whatever types I provide there). |
Just added a fix, let me know if it works! |
almost there! I just noticed that $in operator is not available anymore ;p Here's the example object I'm trying to test against: const obj = {
string: 'foo',
number: 123,
date: new Date(),
boolean: true,
arrayOfNumbers: [1, 2, 3],
arrayOfStrings: ['a', 'b', 'c'],
nestedArrayOfNumbers: [[1], [2], [3]],
doubleNested: [[[1], [2]], [[1, 2]]],
tripleNested: [[[[1]]]],
arrayOfObjects: [{ a: 1 }, { a: 2 }],
nestedArrayOfObjects: [[{ a: 1 }], [{ a: 2 }], [{ a: 3 }]],
nested: {
prop: true,
},
foo: {
bar: {
baz: 1,
},
},
}; query: [obj].filter(sift({
number: {
$in: [123, 2, 3], // Type '{ $in: number[]; }' is not assignable to type 'number | alueQuery<number> undefined'. Object literal may only specify known properties, and '$in' does not exist in type 'ValueQuery<number>'.
},
})); EDIT: also $nin is missing as well, haven't tested all of them, just a heads up :) |
also! I think publishing these changes could break somebody's project in TS, because they might expect |
Hmm, think this change should be a Changes are up, let me know if they look good! |
Everybody should have types matched anyway, if it's not then most likely it's a bug in their codebase (if I'm not wrong 😅) I'll check it out, thanks! |
almost! the commented props in the code below works, however when I tried to use $in operator inside the nested props, it failed the check: [obj].some(sift({
// string: 'foo',
// number: 123,
// date: new Date(0),
// boolean: true,
// arrayOfNumbers: [1, 2, 3],
// arrayOfStrings: ['a', 'b', 'c'],
// nestedArrayOfNumbers: [[1], [2], [3]],
// doubleNested: [[[1], [2]], [[1, 2]]],
// tripleNested: [[[[1]]]],
// arrayOfObjects: [{ a: 1 }, { a: 2 }],
// nestedArrayOfObjects: [[{ a: 1 }], [{ a: 2 }], [{ a: 3 }]],
// nested: {
// prop: true,
// },
foo: {
bar: {
baz: {
$in: [1, 2, 3],
},
},
},
})); // returns false $in or $nin, they both return false. however if I specify strictly that it is equal to 1, then it works. |
Types around that should be fixed. You can't actually nest queries like that FYI |
Oh, then it's all good, works for me :p (haven't used MongoDB queries for like ages). EDIT: Thanks a lot! |
Sorry, now this example is broken from the docs: [
{ tags: ["books", "programming", "travel"] },
{ tags: ["travel", "cooking"] }
].filter(sift({ tags: { $all: ["books", "programming"] } })); ts output:
|
K, try now! |
two issues:
["craig", "john", "jake"].filter(sift(/^j/)); // Type 'RegExp' has no properties in common with type 'BasicValueQuery<string>'.
["craig", "tim", "jake"].filter(sift({ $not: { $in: ["craig", "tim"] } })); ts output
|
Getting types correct around this is so finicky, thanks for bearing with me 🙈. I added all examples to the Changes have been published. |
Thank you! Unfortunately, I'm out of time right now, but I'll test it tomorrow if you don't mind. Cheers. |
No worries, thanks for taking the time to test! |
Everything seems to work well, except one last part: [{ name: "frank" }, { name: "joe" }].filter(
sift({
$where: function() {
return this.name === "frank";
}
})
); TS output:
happens when you type Query<{ name: string; }> This is the final test I've made. I haven't really used much in the real app yet, just examples from the docs & minimal predictions. |
Odd, I'm unable to reproduce that particular bug. In any case, I added more type safety around |
Yup, it got fixed! :) Thanks a lot! |
Awesome! Glad to hear that. Finally I can close this ticket. 👏 |
CC @stalniy, thought you might be interested in this. |
Hi there, first of all, thank you for the awesome work!
The issue -- is it possible to make Query to be a generic interface?
e.g. I am defining a generic repository that accepts a filter parameter that should be Query type of T.
Right now, Query (and it's properties) accept 'any' as a value.
Would be great to have:
or when filtering arrays:
Thank you.
The text was updated successfully, but these errors were encountered: