Skip to content
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

Feat: Type distinct() #54

Merged
merged 4 commits into from
Oct 28, 2024
Merged

Conversation

cau777
Copy link
Contributor

@cau777 cau777 commented Oct 28, 2024

Typing the distinct() function to avoid any as return type

Comment on lines 91 to 118
type DistinctSampleType = {
_id: ObjectId
str: string
nested: {
field: number
}
}

type TestCollection = TsReadWriteCollection<
DistinctSampleType & { writeOnly: boolean },
DistinctSampleType & { readOnly: boolean }
>

const distinctString = (c: TestCollection) => c.distinct('str', {})
const distinctId = (c: TestCollection) => c.distinct('_id', {})
const distinctNested = (c: TestCollection) => c.distinct('nested.field', {})
const distinctReadOnly = (c: TestCollection) => c.distinct('readOnly', {})

ta.assert<ta.Equal<Awaited<ReturnType<typeof distinctString>>, string[]>>()
ta.assert<ta.Equal<Awaited<ReturnType<typeof distinctId>>, ObjectId[]>>()
ta.assert<ta.Equal<Awaited<ReturnType<typeof distinctNested>>, number[]>>()
ta.assert<ta.Equal<Awaited<ReturnType<typeof distinctReadOnly>>, boolean[]>>()

// @ts-expect-error
const _distinctIncorrect = (c: TestCollection) => c.distinct('no', {})

// @ts-expect-error
const _distinctWriteOnly = (c: TestCollection) => c.distinct('writeOnly', {})
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move this to a separate test file, collection.distinct.assert.ts? It does not share anything with the tests above, so it's easier to read in a separate file.

@nicolassanmar nicolassanmar merged commit d61a827 into tianhuil:main Oct 28, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants