typescript - 扩展类型的原型
问题描述
我正在使用 Vue3 和 Typescript。基本上,我想要实现的是:
export type PersonId = string;
export interface Person {
id: PersonId,
lastSeen: Date
}
export type PersonOrId = Person | PersonId;
PersonOrId.prototype.collapse = function (): PersonId {
let id: PersonId;
if (this.hasOwnProperty('id'))
id = (this as Person).id;
else
id = this as PersonId;
return id;
}
然后稍后在代码中使用该函数来保持代码更清洁:
let result: PersonOrId = ...
let id = result.collapse()
但事实证明,你不能prototype
一个type
. 我可以使用 解决这个问题interface
吗?如果是这样,怎么做?
解决方案
推荐阅读
- firebase - FCM(Firebase Cloud Messaging)可以重新分配相同的令牌ID吗
- python - 我如何将 sql 查询传递到 python 脚本中,该脚本使用我的所有登录凭据连接到 sql oracle developer”?
- java - Java:通过分隔符将字符串拆分为哈希表
- ruby - 将块作为变量传递失败“ArgumentError:缺少块”
- matlab - 基本 Matlab for 循环
- events - 如何在实体 AFRAME 上进行焦点事件?
- spring-boot - 在运行时根据文件扩展名设置目标位置
- c# - 如何在 SQL Server 通信中禁用 C# 中的 NetBios
- sql-server - 如何修复 System.Data.SqlClient.SqlException:“用户“xx\xxxx$”登录失败”
- c - 结构填充的规则是什么