refactor(client): type assertion inside User constructor

This commit is contained in:
teidesu 2021-04-26 23:26:57 +03:00
parent 5fee1d0660
commit 85671f6e6a
8 changed files with 18 additions and 15 deletions

View file

@ -57,5 +57,5 @@ export async function addContact(
this._handleUpdate(res) this._handleUpdate(res)
return new User(this, res.users[0] as tl.RawUser) return new User(this, res.users[0])
} }

View file

@ -70,7 +70,7 @@ export async function deleteContacts(
this._handleUpdate(res) 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 return single ? users[0] : users
} }

View file

@ -16,5 +16,5 @@ export async function getContacts(
}) })
assertTypeIs('getContacts', res, 'contacts.contacts') 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))
} }

View file

@ -51,6 +51,6 @@ export async function getUsers(
res = res.filter((it) => it._ !== 'userEmpty') res = res.filter((it) => it._ !== 'userEmpty')
return isArray return isArray
? res.map((it) => new User(this, it as tl.RawUser)) ? res.map((it) => new User(this, it))
: new User(this, res[0] as tl.RawUser) : new User(this, res[0])
} }

View file

@ -352,7 +352,7 @@ export class Message {
} else if (from._ === 'peerUser') { } else if (from._ === 'peerUser') {
this._sender = new User( this._sender = new User(
this.client, this.client,
this._users[from.userId] as tl.RawUser this._users[from.userId]
) )
} else } else
throw new MtCuteTypeAssertionError( throw new MtCuteTypeAssertionError(
@ -420,7 +420,7 @@ export class Message {
} else if (fwd.fromId._ === 'peerUser') { } else if (fwd.fromId._ === 'peerUser') {
sender = new User( sender = new User(
this.client, this.client,
this._users[fwd.fromId.userId] as tl.RawUser this._users[fwd.fromId.userId]
) )
} else } else
throw new MtCuteTypeAssertionError( throw new MtCuteTypeAssertionError(
@ -478,7 +478,7 @@ export class Message {
} else { } else {
this._viaBot = new User( this._viaBot = new User(
this.client, this.client,
this._users[this.raw.viaBotId] as tl.RawUser this._users[this.raw.viaBotId]
) )
} }
} }

View file

@ -61,12 +61,12 @@ export class ChatMember {
) )
this._user = new User( this._user = new User(
this.client, this.client,
this._users[this.raw.peer.userId] as tl.RawUser this._users[this.raw.peer.userId]
) )
} else { } else {
this._user = new User( this._user = new User(
this.client, 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._invitedBy = new User(
this.client, this.client,
this._users[this.raw.inviterId] as tl.RawUser this._users[this.raw.inviterId]
) )
} else { } else {
this._invitedBy = null this._invitedBy = null
@ -179,7 +179,7 @@ export class ChatMember {
if (this.raw._ === 'channelParticipantAdmin') { if (this.raw._ === 'channelParticipantAdmin') {
this._promotedBy = new User( this._promotedBy = new User(
this.client, this.client,
this._users[this.raw.promotedBy] as tl.RawUser this._users[this.raw.promotedBy]
) )
} else { } else {
this._promotedBy = null this._promotedBy = null
@ -200,7 +200,7 @@ export class ChatMember {
if (this.raw._ === 'channelParticipantBanned') { if (this.raw._ === 'channelParticipantBanned') {
this._restrictedBy = new User( this._restrictedBy = new User(
this.client, this.client,
this._users[this.raw.kickedBy] as tl.RawUser this._users[this.raw.kickedBy]
) )
} else { } else {
this._restrictedBy = null this._restrictedBy = null

View file

@ -80,7 +80,7 @@ export class ChatPreview {
if (!this._someMembers) { if (!this._someMembers) {
this._someMembers = this.invite.participants this._someMembers = this.invite.participants
? this.invite.participants.map( ? this.invite.participants.map(
(it) => new User(this.client, it as tl.RawUser) (it) => new User(this.client, it)
) )
: [] : []
} }

View file

@ -3,6 +3,7 @@ import { TelegramClient } from '../../client'
import { ChatPhoto } from './chat-photo' import { ChatPhoto } from './chat-photo'
import { MtCuteArgumentError } from '../errors' import { MtCuteArgumentError } from '../errors'
import { makeInspectable } from '../utils' import { makeInspectable } from '../utils'
import { assertTypeIs } from '../../utils/type-assertion'
export namespace User { export namespace User {
/** /**
@ -41,7 +42,9 @@ export class User {
*/ */
private _user: tl.RawUser 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.client = client
this._user = user this._user = user
} }