Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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
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