Skip to content

Commit

Permalink
feat(core): narrow parsedArgv.session typings
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Sep 4, 2020
1 parent cd2267a commit c42bf77
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
8 changes: 6 additions & 2 deletions packages/koishi-core/src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export interface ParsedLine<O = {}> {

export interface ParsedArgv<U extends User.Field = never, G extends Group.Field = never, O = {}> extends Partial<ParsedLine<O>> {
command: Command<U, G, O>
session: Session<U, G>
session: ArgvSession<U, G, O>
next?: NextFunction
}

Expand All @@ -93,6 +93,10 @@ export interface ExecuteArgv extends Partial<ParsedLine> {
next?: NextFunction
}

export interface ArgvSession<U extends User.Field, G extends Group.Field, O extends {}> extends Session<U, G> {
$argv?: ParsedArgv<U, G, O>
}

export interface CommandConfig<U extends User.Field = never, G extends Group.Field = never> {
/** description */
description?: string
Expand All @@ -107,7 +111,7 @@ export type FieldCollector<T extends TableType, K = keyof Tables[T], O extends {
| ((argv: ParsedArgv<never, never, O>, fields: Set<keyof Tables[T]>) => void)

export type CommandAction<U extends User.Field = never, G extends Group.Field = never, O = {}> =
(this: Command<U, G>, config: ParsedArgv<U, G, O>, ...args: string[]) => void | string | Promise<void | string>
(this: Command<U, G, O>, argv: ParsedArgv<U, G, O>, ...args: string[]) => void | string | Promise<void | string>

export class Command<U extends User.Field = never, G extends Group.Field = never, O = {}> {
config: CommandConfig<U, G>
Expand Down
6 changes: 3 additions & 3 deletions packages/koishi-core/src/plugins/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import { Message } from './message'
export type UserType<T, U extends User.Field = User.Field> = T | ((user: Pick<User, U>) => T)

declare module '../command' {
interface Command<U, G> {
_checkers: ((session: Session<U, G>) => string | boolean)[]
before(checker: (session: Session<U, G>) => string | boolean): this
interface Command<U, G, O> {
_checkers: ((session: ArgvSession<U, G, O>) => string | boolean)[]
before(checker: (session: ArgvSession<U, G, O>) => string | boolean): this
getConfig<K extends keyof CommandConfig>(key: K, session: Session): Exclude<CommandConfig[K], (user: User) => any>
}

Expand Down

0 comments on commit c42bf77

Please sign in to comment.