typings: Collector filter parameter inference (#6574)

This commit is contained in:
Suneet Tipirneni
2021-09-01 06:07:26 -04:00
committed by GitHub
parent d16ada9708
commit 08419561ed
2 changed files with 60 additions and 10 deletions

23
typings/index.d.ts vendored
View File

@@ -1149,7 +1149,7 @@ type InteractionCollectorReturnType<T extends MessageComponentType | MessageComp
? ConditionalInteractionCollectorType<MappedInteractionCollectorOptions[T]> ? ConditionalInteractionCollectorType<MappedInteractionCollectorOptions[T]>
: InteractionCollector<MessageComponentInteraction>; : InteractionCollector<MessageComponentInteraction>;
type InteractionReturnType<T extends MessageComponentType | MessageComponentTypes | undefined> = T extends type InteractionExtractor<T extends MessageComponentType | MessageComponentTypes | undefined> = T extends
| MessageComponentType | MessageComponentType
| MessageComponentTypes | MessageComponentTypes
? MappedInteractionCollectorOptions[T] extends InteractionCollectorOptions<infer Item> ? MappedInteractionCollectorOptions[T] extends InteractionCollectorOptions<infer Item>
@@ -1157,13 +1157,16 @@ type InteractionReturnType<T extends MessageComponentType | MessageComponentType
: never : never
: MessageComponentInteraction; : MessageComponentInteraction;
type MessageCollectorOptionsParams<T> = type MessageCollectorOptionsParams<T extends MessageComponentType | MessageComponentTypes | undefined> =
| ({ componentType?: T } & InteractionCollectorOptionsResolvable) | {
| InteractionCollectorOptions<MessageComponentInteraction>; componentType?: T;
} & InteractionCollectorOptions<InteractionExtractor<T>>;
type AwaitMessageCollectorOptionsParams<T> = type AwaitMessageCollectorOptionsParams<T extends MessageComponentType | MessageComponentTypes | undefined> =
| ({ componentType?: T } & Pick<InteractionCollectorOptionsResolvable, keyof AwaitMessageComponentOptions<any>>) | { componentType?: T } & Pick<
| AwaitMessageComponentOptions<MessageComponentInteraction>; InteractionCollectorOptions<InteractionExtractor<T>>,
keyof AwaitMessageComponentOptions<any>
>;
export class Message extends Base { export class Message extends Base {
public constructor(client: Client, data: RawMessageData); public constructor(client: Client, data: RawMessageData);
@@ -1212,9 +1215,9 @@ export class Message extends Base {
public webhookId: Snowflake | null; public webhookId: Snowflake | null;
public flags: Readonly<MessageFlags>; public flags: Readonly<MessageFlags>;
public reference: MessageReference | null; public reference: MessageReference | null;
public awaitMessageComponent<T extends MessageComponentType | MessageComponentTypes | undefined = undefined>( public awaitMessageComponent<
options?: AwaitMessageCollectorOptionsParams<T>, T extends MessageComponentType | MessageComponentTypes | undefined = MessageComponentTypes.ACTION_ROW,
): Promise<InteractionReturnType<T>>; >(options?: AwaitMessageCollectorOptionsParams<T>): Promise<InteractionExtractor<T>>;
public awaitReactions(options?: AwaitReactionsOptions): Promise<Collection<Snowflake | string, MessageReaction>>; public awaitReactions(options?: AwaitReactionsOptions): Promise<Collection<Snowflake | string, MessageReaction>>;
public createReactionCollector(options?: ReactionCollectorOptions): ReactionCollector; public createReactionCollector(options?: ReactionCollectorOptions): ReactionCollector;
public createMessageComponentCollector< public createMessageComponentCollector<

View File

@@ -510,6 +510,53 @@ client.on('messageCreate', message => {
// Verify that additional options don't affect default collector types. // Verify that additional options don't affect default collector types.
const semiDefaultCollector = message.createMessageComponentCollector({ interactionType: 'APPLICATION_COMMAND' }); const semiDefaultCollector = message.createMessageComponentCollector({ interactionType: 'APPLICATION_COMMAND' });
assertType<InteractionCollector<MessageComponentInteraction>>(semiDefaultCollector); assertType<InteractionCollector<MessageComponentInteraction>>(semiDefaultCollector);
// Make sure filter parameters are properly inferred.
message.createMessageComponentCollector({
filter: i => {
assertType<MessageComponentInteraction>(i);
return true;
},
});
message.createMessageComponentCollector({
componentType: 'BUTTON',
filter: i => {
assertType<ButtonInteraction>(i);
return true;
},
});
message.createMessageComponentCollector({
componentType: 'SELECT_MENU',
filter: i => {
assertType<SelectMenuInteraction>(i);
return true;
},
});
message.awaitMessageComponent({
filter: i => {
assertType<MessageComponentInteraction>(i);
return true;
},
});
message.awaitMessageComponent({
componentType: 'BUTTON',
filter: i => {
assertType<ButtonInteraction>(i);
return true;
},
});
message.awaitMessageComponent({
componentType: 'SELECT_MENU',
filter: i => {
assertType<SelectMenuInteraction>(i);
return true;
},
});
}); });
client.on('interaction', async interaction => { client.on('interaction', async interaction => {