feat(bot): Remove some clutter from desired proprerties intellisense (#4050)

* Remove TBot, Use SetupDesiredProps

* Remove even more clutter from intellisense

* Use type instead of interfaces for some types

This avoids the need for mapped types in Bot

* docs: add info about removing ts clutter to desired props guide

* docs: improve ts clutter section in desired props guide more

---------

Co-authored-by: Awesome Stickz <awesome@stickz.dev>
This commit is contained in:
Fleny
2024-12-28 23:08:15 +05:30
committed by GitHub
co-authored by Awesome Stickz
parent 6abf453319
commit 41219d5417
7 changed files with 501 additions and 345 deletions
+52 -1
View File
@@ -89,7 +89,7 @@ const bot = createBot({
message: {
id: true,
author: true,
}
},
user: {
id: true,
toggles: true, // Toggles includes the "bot" flag
@@ -156,3 +156,54 @@ All the "undesired" properties will be typed with a string that will explain why
The caveats of this behavior are the following:
- Typescript may not always error on the usage of undesired properties, as in some cases, strings can be a valid option (e.g. channel.name is always a string so typescript won't error)
### Removing TypeScript Clutter
Since we dynamically change types based on the desired properties you provide, many functions' types become cluttered. For example, the intellisense for `bot.helpers.getUser()` shows:
```js
(property) getUser: (id: BigString) => Promise<SetupDesiredProps<User, CompleteDesiredProperties<{
message: {
id: true;
author: true;
};
user: {
id: true;
toggles: true;
username: true;
};
}>, DesiredPropertiesBehavior.RemoveKey>>
```
This will become increasingly cluttered as you add more desired properties, making it harder to read and work with. To address this issue, you can do something like:
```js
import { createBot, createDesiredPropertiesObject } from '@discordeno/bot';
const desiredProperties = createDesiredPropertiesObject({
message: {
id: true,
author: true,
},
user: {
id: true,
toggles: true, // Toggles includes the "bot" flag
username: true,
},
})
interface BotDesiredProperties extends Required<typeof desiredProperties> {}
const bot = createBot<BotDesiredProperties>({
// Your usual createBot options, such as token and intents
desiredProperties,
});
```
Now, when you hover over `bot.helpers.getUser()`, you'll see:
```js
(property) getUser: (id: BigString) => Promise<SetupDesiredProps<User, BotDesiredProperties, DesiredPropertiesBehavior.RemoveKey>>
```
This makes it more readable and easier to work with.