Skip to content

Commit 753ba9c

Browse files
authored
Typing updates (telegraf#993)
* chore(npm): dont create an package-lock in the first place * chore(typings): dont explicitly list only a few files * fix(typings): ctx.match can be null normaliseTriggers and RegExp.exec can both return null * fix(typings): branch predicate function can return Promise * fix(typings): MessageMedia.media can be InputFile * fixup! chore(typings): dont explicitly list only a few files * fix(typings): hears function has generic type * feat(typings): export interfaces facing public d.ts files only include public facing interfaces...
1 parent 6cba59d commit 753ba9c

7 files changed

Lines changed: 35 additions & 28 deletions

File tree

.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock=false

tsconfig.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@
1616
"noEmit": true,
1717
"forceConsistentCasingInFileNames": true
1818
},
19-
"files": [
20-
"typings/index.d.ts",
21-
"typings/telegram-types.d.ts",
22-
"typings/test.ts"
19+
"include": [
20+
"typings"
2321
]
2422
}

typings/composer.d.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
import * as tt from './telegram-types.d'
44
import { TelegrafContext } from './context'
55

6-
type HearsTriggers = string[] | string | RegExp | RegExp[] | Function
6+
type HearsTriggers<TContext> = string[] | string | RegExp | RegExp[] | ((value: string, ctx: TContext) => RegExpExecArray | null)
7+
type BranchPredicate<TContext> = boolean | ((ctx: TContext) => boolean | Promise<boolean>)
78

89
export interface MiddlewareFn<TContext extends TelegrafContext> {
910
/*
@@ -51,15 +52,15 @@ export declare class Composer<TContext extends TelegrafContext>
5152
* Registers middleware for handling text messages.
5253
*/
5354
hears(
54-
triggers: HearsTriggers,
55+
triggers: HearsTriggers<TContext>,
5556
...middlewares: ReadonlyArray<Middleware<TContext>>
5657
): this
5758

5859
/**
5960
* Registers middleware for handling callbackQuery data with regular expressions
6061
*/
6162
action(
62-
triggers: HearsTriggers,
63+
triggers: HearsTriggers<TContext>,
6364
...middlewares: ReadonlyArray<Middleware<TContext>>
6465
): this
6566

@@ -111,15 +112,15 @@ export declare class Composer<TContext extends TelegrafContext>
111112
* Generates middleware for handling matching text messages.
112113
*/
113114
static hears<TContext extends TelegrafContext>(
114-
triggers: HearsTriggers,
115+
triggers: HearsTriggers<TContext>,
115116
...middlewares: ReadonlyArray<Middleware<TContext>>
116117
): MiddlewareFn<TContext>
117118

118119
/**
119120
* Generates middleware for handling matching callback queries.
120121
*/
121122
static action<TContext extends TelegrafContext>(
122-
triggers: HearsTriggers,
123+
triggers: HearsTriggers<TContext>,
123124
...middlewares: ReadonlyArray<Middleware<TContext>>
124125
): MiddlewareFn<TContext>
125126

@@ -138,23 +139,30 @@ export declare class Composer<TContext extends TelegrafContext>
138139
* @param middleware middleware to run if the predicate returns true
139140
*/
140141
static optional<TContext extends TelegrafContext>(
141-
test: boolean | ((ctx: TContext) => boolean),
142+
predicate: BranchPredicate<TContext>,
142143
...middlewares: ReadonlyArray<Middleware<TContext>>
143144
): MiddlewareFn<TContext>
144145

145146
/**
146147
* Generates filter middleware.
147148
*/
148149
static filter<TContext extends TelegrafContext>(
149-
test: boolean | ((ctx: TContext) => boolean)
150+
predicate: BranchPredicate<TContext>
150151
): MiddlewareFn<TContext>
151152

153+
/**
154+
* Generates drop middleware.
155+
*/
156+
static drop<TContext extends TelegrafContext>(
157+
predicate: BranchPredicate<TContext>
158+
): Middleware<TContext>
159+
152160
/**
153161
* @param trueMiddleware middleware to run if the predicate returns true
154162
* @param falseMiddleware middleware to run if the predicate returns false
155163
*/
156164
static branch<TContext extends TelegrafContext>(
157-
test: boolean | ((ctx: TContext) => boolean),
165+
predicate: BranchPredicate<TContext>,
158166
trueMiddleware: Middleware<TContext>,
159167
falseMiddleware: Middleware<TContext>
160168
): MiddlewareFn<TContext>

typings/context.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export declare class TelegrafContext {
1919
editedMessage?: tt.Message
2020
from?: tt.User
2121
inlineQuery?: tt.InlineQuery
22-
match?: RegExpExecArray
22+
match?: RegExpExecArray | null
2323
me?: string
2424
message?: tt.IncomingMessage
2525
preCheckoutQuery?: tt.PreCheckoutQuery

typings/markup.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export interface PayButton {
6969
hide: boolean
7070
}
7171

72-
interface LoginUrl {
72+
export interface LoginUrl {
7373
url: string
7474
forward_text?: string
7575
bot_username?: string
@@ -91,7 +91,7 @@ export type InlineKeyboardButton =
9191
| PayButton
9292
| LoginButton
9393

94-
interface KeyboardOptions<TBtn> {
94+
export interface KeyboardOptions<TBtn> {
9595
columns?: number
9696
wrap?(btn: TBtn, index: number, currentRow: TBtn[]): boolean
9797
}

typings/telegraf.d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { TelegrafContext } from './context'
1313
import { Composer } from './composer'
1414
import { Telegram, TelegramOptions } from './telegram'
1515

16-
interface TelegrafOptions {
16+
export interface TelegrafOptions {
1717
/**
1818
* Telegram options
1919
*/
@@ -25,7 +25,7 @@ interface TelegrafOptions {
2525
username?: string
2626
}
2727

28-
interface LaunchPollingOptions {
28+
export interface LaunchPollingOptions {
2929
/**
3030
* Poll timeout in seconds
3131
*/
@@ -47,7 +47,7 @@ interface LaunchPollingOptions {
4747
stopCallback?: () => void | null
4848
}
4949

50-
interface LaunchWebhookOptions {
50+
export interface LaunchWebhookOptions {
5151
/**
5252
* Public domain for webhook. If domain is not specified, hookPath should contain a domain name as well (not only path component).
5353
*/
@@ -185,7 +185,7 @@ export declare class Telegraf<
185185
catch(logFn?: Function): void
186186
}
187187

188-
interface TOptions {
188+
export interface TOptions {
189189
/**
190190
* Telegram options
191191
*/

typings/telegram-types.d.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,14 @@ export type MessageMedia =
9393

9494
export interface InputMediaPhoto {
9595
type: string
96-
media: string
96+
media: InputFile
9797
caption?: string
9898
parse_mode?: string
9999
}
100100

101101
export interface InputMediaVideo {
102102
type: string
103-
media: string
103+
media: InputFile
104104
thumb?: string | InputFile
105105
caption?: string
106106
parse_mode?: string
@@ -112,7 +112,7 @@ export interface InputMediaVideo {
112112

113113
export interface InputMediaAnimation {
114114
type: string
115-
media: string
115+
media: InputFile
116116
thumb?: string | InputFile
117117
caption?: string
118118
parse_mode?: string
@@ -124,7 +124,7 @@ export interface InputMediaAnimation {
124124

125125
export interface InputMediaAudio {
126126
type: string
127-
media: string
127+
media: InputFile
128128
thumb?: string | InputFile
129129
caption?: string
130130
parse_mode?: string
@@ -136,7 +136,7 @@ export interface InputMediaAudio {
136136

137137
export interface InputMediaDocument {
138138
type: string
139-
media: string
139+
media: InputFile
140140
thumb?: string | InputFile
141141
caption?: string
142142
parse_mode?: string
@@ -150,19 +150,19 @@ export interface StickerData {
150150

151151
type FileId = string
152152

153-
interface InputFileByPath {
153+
export interface InputFileByPath {
154154
source: string
155155
}
156156

157-
interface InputFileByReadableStream {
157+
export interface InputFileByReadableStream {
158158
source: NodeJS.ReadableStream
159159
}
160160

161-
interface InputFileByBuffer {
161+
export interface InputFileByBuffer {
162162
source: Buffer
163163
}
164164

165-
interface InputFileByURL {
165+
export interface InputFileByURL {
166166
url: string
167167
filename: string
168168
}

0 commit comments

Comments
 (0)