refactor(client): type assertion inside User constructor
This commit is contained in:
parent
5fee1d0660
commit
85671f6e6a
8 changed files with 18 additions and 15 deletions
|
@ -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])
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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))
|
||||
}
|
||||
|
|
|
@ -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])
|
||||
}
|
||||
|
|
|
@ -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]
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
)
|
||||
: []
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue