From 26d1175b261a81bf6f7fe8575a85aac4acbb4f68 Mon Sep 17 00:00:00 2001 From: alina sireneva Date: Mon, 19 Aug 2024 14:08:49 +0300 Subject: [PATCH] build: don't use stc when building --- packages/convert/src/gramjs/serialize.ts | 2 +- packages/convert/src/telethon/serialize.ts | 2 +- packages/core/src/highlevel/worker/invoker.ts | 2 +- packages/core/src/storage/memory/driver.ts | 2 +- .../core/src/utils/type-assertions.test.ts | 19 ---------------- packages/core/src/utils/type-assertions.ts | 22 ++----------------- .../src/context/business-message.ts | 2 +- .../dispatcher/src/context/callback-query.ts | 6 ++--- .../src/context/chat-join-request.ts | 2 +- .../src/context/chosen-inline-result.ts | 2 +- .../dispatcher/src/context/inline-query.ts | 2 +- packages/dispatcher/src/context/message.ts | 2 +- .../src/context/pre-checkout-query.ts | 2 +- packages/tl/scripts/fetch-api.ts | 10 ++++----- scripts/build-package.js | 11 ---------- 15 files changed, 20 insertions(+), 68 deletions(-) diff --git a/packages/convert/src/gramjs/serialize.ts b/packages/convert/src/gramjs/serialize.ts index 49fc155c..4c0dc98e 100644 --- a/packages/convert/src/gramjs/serialize.ts +++ b/packages/convert/src/gramjs/serialize.ts @@ -4,7 +4,7 @@ import { dataViewFromBuffer } from '@mtcute/core/utils.js' import type { TelethonSession } from '../telethon/types.js' -export function serializeGramjsSession(session: TelethonSession) { +export function serializeGramjsSession(session: TelethonSession): string { if (session.authKey.length !== 256) { throw new MtArgumentError('authKey must be 256 bytes long') } diff --git a/packages/convert/src/telethon/serialize.ts b/packages/convert/src/telethon/serialize.ts index bacd7a36..4b2957c7 100644 --- a/packages/convert/src/telethon/serialize.ts +++ b/packages/convert/src/telethon/serialize.ts @@ -6,7 +6,7 @@ import { serializeIpv4ToBytes, serializeIpv6ToBytes } from '../utils/ip.js' import type { TelethonSession } from './types.js' -export function serializeTelethonSession(session: TelethonSession) { +export function serializeTelethonSession(session: TelethonSession): string { if (session.authKey.length !== 256) { throw new MtArgumentError('authKey must be 256 bytes long') } diff --git a/packages/core/src/highlevel/worker/invoker.ts b/packages/core/src/highlevel/worker/invoker.ts index fcdc566c..c30036f6 100644 --- a/packages/core/src/highlevel/worker/invoker.ts +++ b/packages/core/src/highlevel/worker/invoker.ts @@ -68,7 +68,7 @@ export class WorkerInvoker { } } - makeBinder(target: InvokeTarget) { + makeBinder(target: InvokeTarget): (method: K, isVoid?: boolean) => T[K] { return (method: K, isVoid = false) => { let fn diff --git a/packages/core/src/storage/memory/driver.ts b/packages/core/src/storage/memory/driver.ts index c92d86f1..c486affe 100644 --- a/packages/core/src/storage/memory/driver.ts +++ b/packages/core/src/storage/memory/driver.ts @@ -3,7 +3,7 @@ import type { IStorageDriver } from '../driver.js' export class MemoryStorageDriver implements IStorageDriver { readonly states: Map = new Map() - getState(repo: string, def: () => T) { + getState(repo: string, def: () => T): T { if (!this.states.has(repo)) { this.states.set(repo, def()) } diff --git a/packages/core/src/utils/type-assertions.test.ts b/packages/core/src/utils/type-assertions.test.ts index e721860b..028f6b76 100644 --- a/packages/core/src/utils/type-assertions.test.ts +++ b/packages/core/src/utils/type-assertions.test.ts @@ -3,7 +3,6 @@ import { describe, expect, it } from 'vitest' import { assertTypeIs, assertTypeIsNot, - hasPresentKey, hasValueAtKey, isPresent, mtpAssertTypeIs, @@ -23,24 +22,6 @@ describe('isPresent', () => { }) }) -describe('hasPresentKey', () => { - it('should return true for objects with present keys', () => { - expect(hasPresentKey('a')({ a: 1 })).toBe(true) - expect(hasPresentKey('a')({ a: 1, b: 2 })).toBe(true) - }) - - it('should return false for objects with undefined/null keys', () => { - expect(hasPresentKey('a')({ a: undefined })).toBe(false) - expect(hasPresentKey('a')({ a: null })).toBe(false) - expect(hasPresentKey('a')({ a: undefined, b: 2 })).toBe(false) - expect(hasPresentKey('a')({ a: null, b: 2 })).toBe(false) - }) - - it('should return false for objects without the key', () => { - expect(hasPresentKey('a')({ b: 2 })).toBe(false) - }) -}) - describe('hasValueAtKey', () => { it('should return true for objects with the correct value', () => { expect(hasValueAtKey('a', 1)({ a: 1 })).toBe(true) diff --git a/packages/core/src/utils/type-assertions.ts b/packages/core/src/utils/type-assertions.ts index 51d7bdee..fd4785e7 100644 --- a/packages/core/src/utils/type-assertions.ts +++ b/packages/core/src/utils/type-assertions.ts @@ -8,25 +8,6 @@ export function isPresent(t: T | undefined | null | void): t is T { return t !== undefined && t !== null } -/** - * Returns a function that can be used to filter down objects - * to the ones that have a defined non-null value under the key `k`. - * - * @example - * ```ts - * const filesWithUrl = files.filter(file => file.url); - * files[0].url // In this case, TS might still treat this as undefined/null - * - * const filesWithUrl = files.filter(hasPresentKey("url")); - * files[0].url // TS will know that this is present - * ``` - */ -export function hasPresentKey(k: K) { - return function (a: T & { [k in K]?: V | null }): a is T & { [k in K]: V } { - return a[k] !== undefined && a[k] !== null - } -} - /** * Returns a function that can be used to filter down objects * to the ones that have a specific value V under a key `k`. @@ -44,7 +25,8 @@ export function hasPresentKey(k: K) { * files[0].imageUrl // TS will know this is present, because already it excluded the other union members. * ``` */ -export function hasValueAtKey(k: K, v: V) { +export function hasValueAtKey(k: K, v: V): +(a: T & { [k in K]: unknown }) => a is T & { [k in K]: V } { return function (a: T & { [k in K]: unknown }): a is T & { [k in K]: V } { return a[k] === v } diff --git a/packages/dispatcher/src/context/business-message.ts b/packages/dispatcher/src/context/business-message.ts index 68428f7c..d90dcdd2 100644 --- a/packages/dispatcher/src/context/business-message.ts +++ b/packages/dispatcher/src/context/business-message.ts @@ -21,7 +21,7 @@ import type { UpdateContext } from './base.js' */ export class BusinessMessageContext extends BusinessMessage implements UpdateContext { // this is primarily for proper types in filters, so don't bother much with actual value - readonly _name = 'new_business_message' + readonly _name = 'new_business_message' as const /** * List of messages in the message group. diff --git a/packages/dispatcher/src/context/callback-query.ts b/packages/dispatcher/src/context/callback-query.ts index b92edd0e..4e57795d 100644 --- a/packages/dispatcher/src/context/callback-query.ts +++ b/packages/dispatcher/src/context/callback-query.ts @@ -10,7 +10,7 @@ import type { UpdateContext } from './base.js' * This is a subclass of {@link CallbackQuery}, so all its fields are also available. */ export class CallbackQueryContext extends CallbackQuery implements UpdateContext { - readonly _name = 'callback_query' + readonly _name = 'callback_query' as const constructor( readonly client: TelegramClient, @@ -67,7 +67,7 @@ export class CallbackQueryContext extends CallbackQuery implements UpdateContext * This is a subclass of {@link InlineCallbackQuery}, so all its fields are also available. */ export class InlineCallbackQueryContext extends InlineCallbackQuery implements UpdateContext { - readonly _name = 'inline_callback_query' + readonly _name = 'inline_callback_query' as const constructor( readonly client: TelegramClient, @@ -100,7 +100,7 @@ export class InlineCallbackQueryContext extends InlineCallbackQuery implements U export class BusinessCallbackQueryContext extends BusinessCallbackQuery implements UpdateContext { - readonly _name = 'business_callback_query' + readonly _name = 'business_callback_query' as const constructor( readonly client: TelegramClient, diff --git a/packages/dispatcher/src/context/chat-join-request.ts b/packages/dispatcher/src/context/chat-join-request.ts index 4f2d73e5..c1a2f29e 100644 --- a/packages/dispatcher/src/context/chat-join-request.ts +++ b/packages/dispatcher/src/context/chat-join-request.ts @@ -11,7 +11,7 @@ import type { UpdateContext } from './base.js' export class ChatJoinRequestUpdateContext extends BotChatJoinRequestUpdate implements UpdateContext { - readonly _name = 'bot_chat_join_request' + readonly _name = 'bot_chat_join_request' as const constructor( readonly client: TelegramClient, diff --git a/packages/dispatcher/src/context/chosen-inline-result.ts b/packages/dispatcher/src/context/chosen-inline-result.ts index 9df5d2be..766e87bb 100644 --- a/packages/dispatcher/src/context/chosen-inline-result.ts +++ b/packages/dispatcher/src/context/chosen-inline-result.ts @@ -12,7 +12,7 @@ import type { UpdateContext } from './base.js' * > Inline feedback in [@BotFather](//t.me/botfather) */ export class ChosenInlineResultContext extends ChosenInlineResult implements UpdateContext { - readonly _name = 'chosen_inline_result' + readonly _name = 'chosen_inline_result' as const constructor( readonly client: TelegramClient, diff --git a/packages/dispatcher/src/context/inline-query.ts b/packages/dispatcher/src/context/inline-query.ts index 6d24134a..3fcf457b 100644 --- a/packages/dispatcher/src/context/inline-query.ts +++ b/packages/dispatcher/src/context/inline-query.ts @@ -10,7 +10,7 @@ import type { UpdateContext } from './base.js' * This is a subclass of {@link InlineQuery}, so all its fields are also available. */ export class InlineQueryContext extends InlineQuery implements UpdateContext { - readonly _name = 'inline_query' + readonly _name = 'inline_query' as const constructor( readonly client: TelegramClient, diff --git a/packages/dispatcher/src/context/message.ts b/packages/dispatcher/src/context/message.ts index f8c06d33..ee3a7468 100644 --- a/packages/dispatcher/src/context/message.ts +++ b/packages/dispatcher/src/context/message.ts @@ -21,7 +21,7 @@ import type { UpdateContext } from './base.js' */ export class MessageContext extends Message implements UpdateContext { // this is primarily for proper types in filters, so don't bother much with actual value - readonly _name = 'new_message' + readonly _name = 'new_message' as const /** * List of messages in the message group. diff --git a/packages/dispatcher/src/context/pre-checkout-query.ts b/packages/dispatcher/src/context/pre-checkout-query.ts index e0fbe14c..eaefa0c6 100644 --- a/packages/dispatcher/src/context/pre-checkout-query.ts +++ b/packages/dispatcher/src/context/pre-checkout-query.ts @@ -9,7 +9,7 @@ import type { UpdateContext } from './base.js' * This is a subclass of {@link PreCheckoutQuery}, so all its fields are also available. */ export class PreCheckoutQueryContext extends PreCheckoutQuery implements UpdateContext { - readonly _name = 'pre_checkout_query' + readonly _name = 'pre_checkout_query' as const constructor( readonly client: TelegramClient, diff --git a/packages/tl/scripts/fetch-api.ts b/packages/tl/scripts/fetch-api.ts index 180ded07..15016c36 100644 --- a/packages/tl/scripts/fetch-api.ts +++ b/packages/tl/scripts/fetch-api.ts @@ -9,7 +9,7 @@ import { join } from 'node:path' import * as readline from 'node:readline' import * as cheerio from 'cheerio' -import { hasPresentKey, isPresent } from '@mtcute/core/utils.js' +import { isPresent } from '@mtcute/core/utils.js' import type { TlEntry, TlFullSchema, @@ -299,17 +299,17 @@ async function main() { } } - const nonEmptyOptions = chooseOptions.filter(hasPresentKey('entry')) + const nonEmptyOptions = chooseOptions.filter(it => it.entry !== undefined) console.log( 'Conflict detected (%s) at %s %s:', mergeError, - nonEmptyOptions[0].entry.kind, - nonEmptyOptions[0].entry.name, + nonEmptyOptions[0].entry!.kind, + nonEmptyOptions[0].entry!.name, ) console.log('0. Remove') nonEmptyOptions.forEach((opt, idx) => { - console.log(`${idx + 1}. ${opt.schema.name}: (${opt.entry.kind}) ${writeTlEntryToString(opt.entry)}`) + console.log(`${idx + 1}. ${opt.schema.name}: (${opt.entry!.kind}) ${writeTlEntryToString(opt.entry!)}`) }) while (true) { diff --git a/scripts/build-package.js b/scripts/build-package.js index 15a8e80c..2ff08093 100644 --- a/scripts/build-package.js +++ b/scripts/build-package.js @@ -5,7 +5,6 @@ import * as path from 'node:path' import * as glob from 'glob' import ts from 'typescript' -import * as stc from '@teidesu/slow-types-compiler' const __dirname = path.dirname(new URL(import.meta.url).pathname) @@ -570,16 +569,6 @@ if (IS_JSR) { ) } } - - console.log('[i] Processing with slow-types-compiler...') - const project = stc.createProject() - stc.processPackage(project, denoJson) - const unsavedSourceFiles = project.getSourceFiles().filter(s => !s.isSaved()) - - if (unsavedSourceFiles.length > 0) { - console.log('[v] Changed %d files', unsavedSourceFiles.length) - project.saveSync() - } } else { // make shims for esnext resolution (that doesn't respect package.json `exports` field) function makeShim(name, target) {