Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:

- uses: denoland/setup-deno@v1
with:
deno-version: v1.x
deno-version: v2.x

- name: Format
run: deno fmt --check
Expand All @@ -23,7 +23,7 @@ jobs:
run: deno lint

- name: Check
run: deno check src/mod.ts
run: deno check --allow-import src/mod.ts

- name: Test
run: deno task test
Expand Down
2 changes: 1 addition & 1 deletion deno.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
},
"tasks": {
"example": "cd examples && deno run --allow-net --allow-read deno.ts",
"test": "deno test --allow-read --allow-run --allow-write",
"test": "deno test --allow-import --allow-read --allow-run --allow-write",
"dnt": "deno run --allow-env --allow-net --allow-read --allow-run --allow-write scripts/dnt.ts"
},
"test": {
Expand Down
12 changes: 6 additions & 6 deletions src/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export class I18n<C extends Context = Context> {
}

/** Returns a middleware to .use on the `Bot` instance. */
middleware(): MiddlewareFn<C & I18nFlavor> {
middleware(): MiddlewareFn<I18nFlavor<C>> {
return middleware(this.fluent, this.config);
}
}
Expand All @@ -123,7 +123,7 @@ function middleware<C extends Context = Context>(
useSession,
globalTranslationContext,
}: I18nConfig<C>,
): MiddlewareFn<C & I18nFlavor> {
): MiddlewareFn<I18nFlavor<C>> {
return async function (ctx, next): Promise<void> {
let translate: TranslateFunction;

Expand Down Expand Up @@ -201,10 +201,10 @@ should either enable sessions or use `ctx.i18n.useLocale()` instead.",
*
* @param key Key of the message to listen for.
*/
export function hears(key: string) {
return function <C extends Context & I18nFlavor>(
ctx: C,
): ctx is HearsContext<C> {
export function hears<C extends Context>(key: string) {
return function <TContext extends I18nFlavor<C>>(
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this is better than removing the type parameter from hears and then doing

Suggested change
return function <TContext extends I18nFlavor<C>>(
return function <C extends Context, CTContext extends I18nFlavor<C>>(

ctx: TContext,
): ctx is HearsContext<TContext> {
const expected = ctx.t(key);
return ctx.hasText(expected);
};
Expand Down
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export type TranslateFunction = <K extends string>(
variables?: TranslationVariables<K>,
) => string;

export interface I18nFlavor {
export type I18nFlavor<C extends Context> = C & {
/** I18n context namespace object */
i18n: {
/** Fluent instance used internally. */
Expand Down Expand Up @@ -76,7 +76,7 @@ export interface I18nFlavor {
translate: TranslateFunction;
/** Translation function bound to the current locale. */
t: TranslateFunction;
}
};

export interface I18nConfig<C extends Context = Context> {
/**
Expand Down
5 changes: 2 additions & 3 deletions tests/bot.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { Bot, Context, session, SessionFlavor } from "./deps.ts";
import { hears, I18n, I18nFlavor } from "../src/mod.ts";
import { Bot, Context, session, SessionFlavor } from "./deps.ts";
import { makeTempLocalesDir } from "./utils.ts";

interface SessionData {
apples: number;
}

type MyContext =
& Context
& I18nFlavor
& I18nFlavor<Context>
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those aren't correctly combined

& SessionFlavor<SessionData>;

export const bot = new Bot<MyContext>("TOKEN");
Expand Down
5 changes: 2 additions & 3 deletions tests/session_bot.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { Bot, Context, session, SessionFlavor } from "./deps.ts";
import { I18n, I18nFlavor } from "../src/mod.ts";
import { Bot, Context, session, SessionFlavor } from "./deps.ts";
import { makeTempLocalesDir } from "./utils.ts";

type SessionData = Record<never, never>;
type MyContext =
& Context
& I18nFlavor
& I18nFlavor<Context>
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

& SessionFlavor<SessionData>;

export const bot = new Bot<MyContext>("TOKEN");
Expand Down