From 85671f6e6ab22907bdea42fdd32fc5774b084474 Mon Sep 17 00:00:00 2001 From: teidesu Date: Mon, 26 Apr 2021 23:26:57 +0300 Subject: [PATCH] refactor(client): type assertion inside User constructor --- packages/client/src/methods/contacts/add-contact.ts | 2 +- .../client/src/methods/contacts/delete-contacts.ts | 2 +- packages/client/src/methods/contacts/get-contacts.ts | 2 +- packages/client/src/methods/users/get-users.ts | 4 ++-- packages/client/src/types/messages/message.ts | 6 +++--- packages/client/src/types/peers/chat-member.ts | 10 +++++----- packages/client/src/types/peers/chat-preview.ts | 2 +- packages/client/src/types/peers/user.ts | 5 ++++- 8 files changed, 18 insertions(+), 15 deletions(-) diff --git a/packages/client/src/methods/contacts/add-contact.ts b/packages/client/src/methods/contacts/add-contact.ts index 145d759e..5e53f8e1 100644 --- a/packages/client/src/methods/contacts/add-contact.ts +++ b/packages/client/src/methods/contacts/add-contact.ts @@ -57,5 +57,5 @@ export async function addContact( this._handleUpdate(res) - return new User(this, res.users[0] as tl.RawUser) + return new User(this, res.users[0]) } diff --git a/packages/client/src/methods/contacts/delete-contacts.ts b/packages/client/src/methods/contacts/delete-contacts.ts index 05af17a5..61fe94e1 100644 --- a/packages/client/src/methods/contacts/delete-contacts.ts +++ b/packages/client/src/methods/contacts/delete-contacts.ts @@ -70,7 +70,7 @@ export async function deleteContacts( this._handleUpdate(res) - const users = res.users.map(user => new User(this, user as tl.RawUser)) + const users = res.users.map(user => new User(this, user)) return single ? users[0] : users } diff --git a/packages/client/src/methods/contacts/get-contacts.ts b/packages/client/src/methods/contacts/get-contacts.ts index 5a929c60..6a632520 100644 --- a/packages/client/src/methods/contacts/get-contacts.ts +++ b/packages/client/src/methods/contacts/get-contacts.ts @@ -16,5 +16,5 @@ export async function getContacts( }) assertTypeIs('getContacts', res, 'contacts.contacts') - return res.users.map((user) => new User(this, user as tl.RawUser)) + return res.users.map((user) => new User(this, user)) } diff --git a/packages/client/src/methods/users/get-users.ts b/packages/client/src/methods/users/get-users.ts index b19220c4..7007a521 100644 --- a/packages/client/src/methods/users/get-users.ts +++ b/packages/client/src/methods/users/get-users.ts @@ -51,6 +51,6 @@ export async function getUsers( res = res.filter((it) => it._ !== 'userEmpty') return isArray - ? res.map((it) => new User(this, it as tl.RawUser)) - : new User(this, res[0] as tl.RawUser) + ? res.map((it) => new User(this, it)) + : new User(this, res[0]) } diff --git a/packages/client/src/types/messages/message.ts b/packages/client/src/types/messages/message.ts index 373f06c0..2806897d 100644 --- a/packages/client/src/types/messages/message.ts +++ b/packages/client/src/types/messages/message.ts @@ -352,7 +352,7 @@ export class Message { } else if (from._ === 'peerUser') { this._sender = new User( this.client, - this._users[from.userId] as tl.RawUser + this._users[from.userId] ) } else throw new MtCuteTypeAssertionError( @@ -420,7 +420,7 @@ export class Message { } else if (fwd.fromId._ === 'peerUser') { sender = new User( this.client, - this._users[fwd.fromId.userId] as tl.RawUser + this._users[fwd.fromId.userId] ) } else throw new MtCuteTypeAssertionError( @@ -478,7 +478,7 @@ export class Message { } else { this._viaBot = new User( this.client, - this._users[this.raw.viaBotId] as tl.RawUser + this._users[this.raw.viaBotId] ) } } diff --git a/packages/client/src/types/peers/chat-member.ts b/packages/client/src/types/peers/chat-member.ts index e0136b4c..64e5412e 100644 --- a/packages/client/src/types/peers/chat-member.ts +++ b/packages/client/src/types/peers/chat-member.ts @@ -61,12 +61,12 @@ export class ChatMember { ) this._user = new User( this.client, - this._users[this.raw.peer.userId] as tl.RawUser + this._users[this.raw.peer.userId] ) } else { this._user = new User( this.client, - this._users[this.raw.userId] as tl.RawUser + this._users[this.raw.userId] ) } } @@ -158,7 +158,7 @@ export class ChatMember { ) { this._invitedBy = new User( this.client, - this._users[this.raw.inviterId] as tl.RawUser + this._users[this.raw.inviterId] ) } else { this._invitedBy = null @@ -179,7 +179,7 @@ export class ChatMember { if (this.raw._ === 'channelParticipantAdmin') { this._promotedBy = new User( this.client, - this._users[this.raw.promotedBy] as tl.RawUser + this._users[this.raw.promotedBy] ) } else { this._promotedBy = null @@ -200,7 +200,7 @@ export class ChatMember { if (this.raw._ === 'channelParticipantBanned') { this._restrictedBy = new User( this.client, - this._users[this.raw.kickedBy] as tl.RawUser + this._users[this.raw.kickedBy] ) } else { this._restrictedBy = null diff --git a/packages/client/src/types/peers/chat-preview.ts b/packages/client/src/types/peers/chat-preview.ts index 4814da84..b53e0bca 100644 --- a/packages/client/src/types/peers/chat-preview.ts +++ b/packages/client/src/types/peers/chat-preview.ts @@ -80,7 +80,7 @@ export class ChatPreview { if (!this._someMembers) { this._someMembers = this.invite.participants ? this.invite.participants.map( - (it) => new User(this.client, it as tl.RawUser) + (it) => new User(this.client, it) ) : [] } diff --git a/packages/client/src/types/peers/user.ts b/packages/client/src/types/peers/user.ts index 811ac6c2..09de7e1b 100644 --- a/packages/client/src/types/peers/user.ts +++ b/packages/client/src/types/peers/user.ts @@ -3,6 +3,7 @@ import { TelegramClient } from '../../client' import { ChatPhoto } from './chat-photo' import { MtCuteArgumentError } from '../errors' import { makeInspectable } from '../utils' +import { assertTypeIs } from '../../utils/type-assertion' export namespace User { /** @@ -41,7 +42,9 @@ export class User { */ private _user: tl.RawUser - constructor(client: TelegramClient, user: tl.RawUser) { + constructor(client: TelegramClient, user: tl.TypeUser) { + assertTypeIs('User#init', user, 'user') + this.client = client this._user = user }