Skip to content

Commit

Permalink
feat(core): optimize typings
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Feb 16, 2020
1 parent 1576873 commit f135818
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions packages/koishi-core/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import { format } from 'util'
export type NextFunction = (next?: NextFunction) => any
export type Middleware = (meta: Meta<'message'>, next: NextFunction) => any

type PluginFunction <T extends Context, U> = (ctx: T, options: U) => void
type PluginObject <T extends Context, U> = { name?: string, apply: PluginFunction<T, U> }
type PluginFunction <T extends Context, U = any> = (ctx: T, options: U) => void
type PluginObject <T extends Context, U = any> = { name?: string, apply: PluginFunction<T, U> }
export type Plugin <T extends Context = Context, U = any> = PluginFunction<T, U> | PluginObject<T, U>

type Subscope = [number[], number[]]
Expand Down Expand Up @@ -127,15 +127,13 @@ export class Context {
})
}

plugin <U> (plugin: PluginFunction<this, U>, options?: U): this
plugin <U> (plugin: PluginObject<this, U>, options?: U): this
plugin <U> (plugin: Plugin<this, U>, options: any) {
plugin <T extends Plugin<this>> (plugin: T, options?: T extends Plugin<this, infer U> ? U : never) {
if (options === false) return
const ctx = Object.create(this)
if (typeof plugin === 'function') {
plugin(ctx, options)
(plugin as PluginFunction<this>)(ctx, options)
} else if (plugin && typeof plugin === 'object' && typeof plugin.apply === 'function') {
plugin.apply(ctx, options)
(plugin as PluginObject<this>).apply(ctx, options)
} else {
throw new Error(errors.INVALID_PLUGIN)
}
Expand Down

0 comments on commit f135818

Please sign in to comment.