-
Couldn't load subscription status.
- Fork 109
Closed
Description
There are currently two problems with the signature of ensureIndex
- A set of types, like
EnsurePersistentIndexOptionsandEnsureGeoIndexOptionsexist, but no union type that combines the options. For example:type EnsureIndexOptions = EnsurePersistentIndexOptions | EnsureGeoIndexOptions | ... - Using overloading on
ensureIndexis unnecessary because the options are fit to be a tagged union.
Because you use overloading (2.) instead of using a union (which would also solve the problem 1.), this is not possible:
// need to combine it when I want to wrap ensureIndex into some logic
type EnsureIndexOptions =
| EnsurePersistentIndexOptions
| EnsureGeoIndexOptions
| EnsureTtlIndexOptions
| EnsureZkdIndexOptions
| EnsureInvertedIndexOptions
function takesEnsureIndexOptions(opts: EnsureIndexOptions) {
// collection API
collection.ensureIndex(opts) // this will fail, because overload signatures are not compatible with unions
}You need to always consider your API is not used inline and the type is inferred, but also wrapped by some consumer logic. The current design is very limited in this way.
Example of the consequences and what is needed because of overloading:
await Promise.all(
indecies.map(indexOpts => {
switch (indexOpts.type) {
case "persistent":
return col.ensureIndex(indexOpts)
case "geo":
return col.ensureIndex(indexOpts)
case "inverted":
return col.ensureIndex(indexOpts)
case "ttl":
return col.ensureIndex(indexOpts)
case "zkd":
return col.ensureIndex(indexOpts)
}
})Its also possible to map the Options to the Return type using conditionals. I assume that was the initial consideration when the overload API was picked? Maybe it was not possible at that time?
Metadata
Metadata
Assignees
Labels
No labels