[英]Generic property 'enabled' does not exist on type 'Node'?
There is a type Node
:有一种类型
Node
:
export type Node<T> = T extends ITreeNode ? T : never;
export interface ITreeNode extends TreeNodeBase<ITreeNode> {
enabled: boolean;
}
export abstract class Tree<Node> {
nodeEnableToggle(node: Node): void {
node.enabled = !node.enabled;
}
}
Why I get this error message:为什么我收到此错误消息:
Property 'enabled' does not exist on type 'Node'.
类型“节点”上不存在属性“启用”。
if T
extends ITreeNode
?如果
T
扩展ITreeNode
?
I also tried this:我也试过这个:
type Node<T> = T & ITreeNode;
initialize(source: Node): void {
this.dataSource.data = source.children;
}
The same error:同样的错误:
Property 'children' does not exist on type 'Node'
“节点”类型上不存在属性“子项”
I have tried to recreate a reproducible example here.我试图在这里重新创建一个可重现的示例。 As far as I understand, if you absolutely have to use conditional type, you may require to pass two parameters to the generic.
据我了解,如果您绝对必须使用条件类型,则可能需要将两个参数传递给泛型。 Both being the implementation of
ITreeNode
.两者都是
ITreeNode
的实现。 There may be a better way to do this without repetition.可能有更好的方法来做到这一点而无需重复。
However, I was wondering what extra goals are you achieving by having conditional type that you cannot do by using non fancy extend
as I have done in Tree1
class below?但是,我想知道通过使用非花式
extend
无法实现的条件类型,您可以实现哪些额外目标,就像我在下面的Tree1
class 中所做的那样?
interface TreeNodeBase<T> {
obj: T;
}
export interface ITreeNode extends TreeNodeBase<ITreeNode> {
enabled: boolean;
}
export type Node<T> = T extends ITreeNode ? T : never;
interface TreeActions<T> {
action: (t: T) => void;
}
// removed abstract to show examples below
export class Tree<U, T extends Node<U>> implements TreeActions<Node<U>> {
nodeEnableToggle(node: T): void {
node.enabled = !node.enabled;
}
action() {}
}
// removed abstract to show examples below
export class Tree1<T extends ITreeNode> implements TreeActions<T> {
nodeEnableToggle(node: T): void {
node.enabled = !node.enabled;
}
action() {}
}
// Examples
interface ATreeNode extends ITreeNode {
extra: number;
}
new Tree<ATreeNode, ATreeNode>();
new Tree1<ATreeNode>();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.