admin管理员组文章数量:1429954
I want to have a function to test if a class variable is not null
and use it in follow up function calls. But got TS plaints. Encapsulate this validate function because I need to call it in many of my methods.
class A {
point: Point | null
validatePoint() {
if (!this.point) throw new Error()
}
update(p: Point | null) {
this.validatePoint()
// ts plains this.point can be null
doSomething(this.point)
}
}
I want to have a function to test if a class variable is not null
and use it in follow up function calls. But got TS plaints. Encapsulate this validate function because I need to call it in many of my methods.
class A {
point: Point | null
validatePoint() {
if (!this.point) throw new Error()
}
update(p: Point | null) {
this.validatePoint()
// ts plains this.point can be null
doSomething(this.point)
}
}
Share
Improve this question
edited Jul 5, 2020 at 18:43
halfer
20.4k19 gold badges109 silver badges202 bronze badges
asked Nov 24, 2019 at 7:50
MengoMengo
1,2771 gold badge12 silver badges28 bronze badges
4 Answers
Reset to default 6Typescript 3.7 introduces assertions in control flow analysis:
class A {
point: Point | null = null;
validatePoint(): asserts this is { point: Point} {
if (!this.point) throw new Error()
}
update(p: Point | null) {
this.validatePoint()
// now ts is sure that this.point can't be null
doSomething(this.point)
}
}
Playground
One option is to use a type guard instead, and throw in update
instead of validatePoint
:
class A {
point: Point | null;
validatePoint(p: Point | null): p is Point {
return Boolean(this.point);
}
update(p: Point | null) {
if (!this.validatePoint(this.point)) {
throw new Error();
}
doSomething(this.point);
}
}
But using a separate method for something like this seems a bit overkill:
update(p: Point | null) {
if (this.point === null) {
throw new Error();
}
doSomething(this.point);
}
If you have to put it in a separate function, then another option is for validatePoint
to return the validated point (since this sort of thing can't be used to narrow the type of a parent object IIRC):
class A {
point: Point | null;
validatePoint() {
if (this.point === null) {
throw new Error();
}
return this.point;
}
update(p: Point | null) {
const validatedPoint = this.validatePoint();
doSomething(validatedPoint);
}
}
You should be explicitly checking for null using an if(this.point === null)
statement. Although I don't think you need a separate method for this, it seems a bit overkill.
In update
, you validate if this.point
is null.
If this.point
is null, an error is thrown and doSomething
is not invoked.
If this.point
is not null, that means this.point
is Point
type.
So it is not possible that this.point
is null, when doSomething
is invoked. That's why TS plains like that.
本文标签: javascriptFunction to test variable type or throw in TypeScriptStack Overflow
版权声明:本文标题:javascript - Function to test variable type or throw in TypeScript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745529965a2662014.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论