typescript - 在 TypeScript 类中,是否有一种干净的方法来处理最初未定义的值?
问题描述
想象一个Connection
使用密码实例化的类,用于从服务器获取令牌。所以
this.password
总是被定义this.token
在我们收到服务器的回复之前是未定义的,但在那之后它总是被定义的
class Connection {
password: string
token?: string
constructor(password: string) {
this.password = password
}
async connect() {
// ... connect to server
this.token = await 'something that we get from the server'
}
doStuff() {
// at this point I know that `this.token` is defined
const something = someUtilityFunction(this.token)
// this will fail because TypeScript still thinks this.token might be undefined
}
}
function someUtilityFunction(token: string) {
// this function requires `token` to be defined
}
// using the class:
const conn = new Connection('foo')
await conn.connect()
conn.doStuff()
我知道我可以断言this.token
用!
.
class Connection {
...
doStuff() {
const something = someUtilityFunction(this.token!)
}
}
我也可以在每个引用之前this.token
加上一个断言函数:
function assert(value: unknown): asserts value {
if (value === undefined) throw new Error('must be defined');
}
...
class Connection {
...
doStuff() {
assert(this.token)
const something = someUtilityFunction(this.token)
}
}
this.token
但是,在我余下的课堂生活中,每次提到这些都必须做这件事,感觉很混乱。
有没有更好的办法?
解决方案
但是,在课堂的余生中,每次我提到 this.token 时都必须做这两个中的任何一个,感觉很混乱。
它并不凌乱。
可选的属性本质上总是可以未定义的,因此在您访问它时需要检查它。
解决方案 1:令牌根本不是可选的
class Connection {
password: string
token: string
constructor(password: string) {
this.password = password;
this.token = '';
}
如果您知道在匿名函数中this.token
应该定义它,那么一开始就没有理由将其未定义。
解决方案 2:断言函数
function getToken(): string {
if(this.token) return this.token;
throw new Error(`Token is not defined`);
}
只需检查this.token
使用它时定义的内容,您将只拥有if
一次。
推荐阅读
- reactjs - 如何在 ant 设计表中混合行跨度和可扩展行
- github - docker push windows 容器镜像到 GitHub 包存储库失败
- python - 从 QWidgetItem 类对象访问“isChecked”属性
- oracle - 保存表单后如何在 Oracle Forms 构建器中创建警报
- spring - 如何使用 Spring 声明式事务和 EntityManagerFactory?
- java - 如何将包含所需参数的列表传递给函数
- javascript - 当此测试失败时,为什么没有正确打印错误消息?
- android - 运行 Android Studio Profiler 时的性能问题:IDE 内存不足
- amazon-web-services - 限制用户仅列出存储桶中的某些文件夹的 AWS S3 策略
- java - 以对象参数为关键的享元设计模式?