#59715·TypeScript

while 循环中未正确引用类型

作者: mozesstumpf创建于 2024年8月22日更新于 2026年9月16日
标签BugHelp WantedDomain: check: Control Flow

class Child extends HTMLElement { get parent() { const { parentElement } = this; if (!(parentElement instanceof Parent)) { return null; } return parentElement; } } class Parent extends HTMLElement { get parent() { const { parentElement } = this; if (!(parentElement instanceof GrandParent)) { return null; } return parentElement; } } class GrandParent extends HTMLElement { get parent() { const { parentElement } = this; if (!(parentElement instanceof GreatGrandParent)) { return null; } return parentElement; } } class GreatGrandParent extends HTMLElement { get parent() { return null; } } // irrelevant code customElements.define("c-child", Child); customElements.define("c-parent", Parent); customElements.define("c-grand-parent", GrandParent); customElements.define("c-great-grand-parent", GreatGrandParent);

type ParentElements = GreatGrandParent | GrandParent | Parent;

const child = new Child();

let currentParent: ParentElements | null = child.parent;

while (currentParent) { console.log(currentParent); // should be GreatGrandParent | GrandParent | Parent currentParent = currentParent.parent; }

// The currentParent's type is not referred correctly. It became Parent | GrandParent.

// The currentParent's type should be Parent | GrandParent | GreatGrandParent.

// Additional information about the issue

内容来源: microsoft/TypeScript