gpt4 book ai didi

javascript - 为什么分配给变量(别名条件)时对类属性的类型缩小检查不起作用?

转载 作者:行者123 更新时间:2023-12-05 00:32:30 25 4
gpt4 key购买 nike

如何缩小别名条件表达式中类的属性类型?
:在一个类方法中,我想做一个类型缩小检查,例如 this._end === null && this._head === null ,但我想先将此检查的结果分配给一个变量,然后再在类型缩小 if 中使用它条款。它没有按预期工作。
不短 :假设我们有一个 Queue 类:

class Queue {
private _length: number = 0;
private _head: Node | null = null;
private _end: Node | null = null;

public enqueue(node: Node): boolean {
this._length += 1;

const isQueueEmpty = this._end === null;
if (isQueueEmpty) {
this._head = node;
this._end = node;
return true;
}

this._end.setLink(node); // TypeScript doesn't understand that this._end passed the null check
// in the if statement and writes that this._end can be null.
this._end = node;
return true;
}
}
如果你直接在 中写入 enqueue 函数中的检查如果 条件,那么 TypeScript 就知道 this._end 不是 :
public enqueue(node: Node): boolean {
this._length += 1;

if (this._end === null) {
this._head = node;
this._end = node;
return true;
}

this._end.setLink(node); // Now TypeScript understand that this._end passed the null check
this._end = node;
return true;
}
但是代码的描述性较差。
此外,总的来说,我需要说类的两个属性都通过了 。空 查看。具体来说:this._head 和 this._end。
我写了以下方法:
type QueuePointerKeys = '_head' | '_end';

private _isQueueEmpty(): this is this & { [K in QueuePointerKeys]: null } {
return this._end === null;
}
它不起作用。如果你把它放在 enqueue 方法中,那么:
public enqueue(node: Node): boolean {
this._length += 1;

if (this._isQueueEmpty()) {
this._head = node; // TypeScript writes that a variable of type null cannot be assigned the type Node
this._end = node; // TypeScript writes that a variable of type null cannot be assigned the type Node
return true;
}

this._end.setLink(node); // TypeScript doesn't understand that this._end passed the null check
// in the if statement and writes that this._end can be null.
this._end = node;
return true;
}
如何使用类方法,例如 _isQueueEmpty类型缩小?这与 this._end === null && this._head === null 类似。直接用作 的条件如果 陈述?

最佳答案

Control Flow Analysis of Aliased Conditions and Discriminants只是最近才添加的,它有一些限制,包括:

Narrowing through indirect references occurs only when the conditional expression or discriminant property access is declared in a const variable declaration with no type annotation, and the reference being narrowed is a const variable, a readonly property, or a parameter for which there are no assignments in the function body.


由于 this._end不是只读属性,它不会为你工作。我通过分配 this._end 确认这是您的问题到本地 const 变量,然后将其用于条件以及 setLink称呼:
interface Node {
setLink(node: Node): void
}

class Queue {
private _length: number = 0;
private _head: Node | null = null;
private _end: Node | null = null;

public enqueue(node: Node): boolean {
this._length += 1;

const end = this._end
const isQueueEmpty = end === null;

if (isQueueEmpty) {
this._head = node;
this._end = node;
return true;
}

end.setLink(node);
this._end = node;
return true;
}

}
事实上,有一个针对您的具体案例的未解决问题: Control flow analysis of aliased conditions is not able to narrow object properties #46412 ,以及 Allow non-readonly properties to be used in aliased conditional expressions #44972 .
让我们希望他们能尽快找到解决方案🤞🏾

关于javascript - 为什么分配给变量(别名条件)时对类属性的类型缩小检查不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70244142/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com