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

[Do Not Merge] PoC for shell-api autocomplete type definitions #1802

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

addaleax
Copy link
Contributor

@addaleax addaleax commented Jan 24, 2024

cd packages/shell-api && \
  npm run compile && \
  npx api-extractor run ; \
  npx ts-node bin/api-postprocess.ts ; \
  cat lib/api-processed.d.ts

(api-extractor will dump a ton of errors)

```sh
cd packages/shell-api && \
  npm run compile && \
  npx api-extractor run ; \
  npx ts-node bin/api-postprocess.ts ; \
  cat lib/api-processed.d.ts
```
@addaleax addaleax added the wip Work in Progress label Jan 24, 2024
getCollection(name: string): Collection {
getCollection<KD extends StringKey<M>, KC extends StringKey<M[KD]>>(
name: `${KD}.${KC}`
): Collection<M, M[KD], M[KD][KC]> {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@kmruiz Wanted to highlight this, doubt many people will actually use .getCollection() on a Mongo object ... but this is interesting, TS is not able to autocomplete the name argument for me (which, like, fair enough), but if I pass a valid <db>.<collection> namespace, it will return a collection with the correct schema, kind of interesting that it only partially works

Copy link
Contributor

Choose a reason for hiding this comment

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

That feels like a bug right? If I recall, it should be able to autocomplete on string templates. But yeah everyone will use db.collection as you said, I don't see any reason now for not suggesting the usage of collections using property access instead of getCollection.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it's struggling a bit with the fact that the second part (collection name) after the . depends on the first part (database name)

Copy link
Contributor

Choose a reason for hiding this comment

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

Makes sense, this seems like a hard problem to solve for a compiler to be fair.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wip Work in Progress
Projects
None yet
2 participants