microsoft / TypeScript Public
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Incorrect circularity detection #33191
Comments
here's another example from zone (source file function shallowObj(obj: {[k: string]: any} | undefined, depth: number): any {
if (!obj || !depth) return null;
const out: {[k: string]: any} = {};
for (const key in obj) {
if (obj.hasOwnProperty(key)) {
let value: any = obj[key];
switch (typeof value) {
case 'object':
const name = value && value.constructor && (<any>value.constructor).name;
value = name == (<any>Object).name ? shallowObj(value, depth - 1) : name;
break;
case 'function':
value = value.name || undefined;
break;
}
out[key] = value;
}
}
return out;
} Gives:
|
Another example i've just come across, TypeScript version 2.8.3: function testFunction() {
let state: { value: number } | null = null;
for (let i = 0; i < 10; i++) {
const previousState = state;
// ^^^^^^^^^^^^^ error: 'previousState' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.ts(7022)
state = null;
const value = i === 0 ? 0 : previousState?.value;
// ^^^^^ error: 'value' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.ts(7022)
if (value !== undefined) {
state = { value: value + 1 }
}
}
console.log('Test Function:', state?.value);
} When putting in explicit type annotations, it produces a new error: function testFunction() {
let state: { value: number } | null = null;
for (let i = 0; i < 10; i++) {
const previousState: { value: number } | null = state;
state = null;
const value: number | undefined = i === 0 ? 0 : previousState?.value;
// Property 'value' does not exist on type 'never'.ts(2339): ^^^^^
if (value !== undefined) {
state = { value: value + 1 }
}
}
console.log('Test Function:', state?.value);
} |
Here's another case, but then with a interface Node {
next: Node | null;
}
function test(node: Node) {
let currentNode: Node | null = node;
while (currentNode !== null) {
const nextNode = currentNode.next;
// ~~~~~~~~ error TS7022: 'nextNode' implicitly has type 'any' because it does not have a type annotation
// and is referenced directly or indirectly in its own initializer.
currentNode = nextNode;
}
} |
BTW, using @JoostK's example, if you removes the asignation, error goes away... interface Node {
next: Node | null;
}
function test(node: Node) {
let currentNode: Node | null = node;
while (currentNode !== null) {
const nextNode = currentNode.next; // No error
// currentNode = nextNode; // Uncomment it, and error comes back
console.log(nextNode);
}
} |
Removing the assignment removes the circular nature of |
TypeScript Version: 3.6.2
Search Terms:
Code
Expected behavior:
No error.
Actual behavior:
I'm really struggling to see where the circularity comes in. The type of
result
is shown as[number, number]
in that branch, so I'd assume destructuring it would result in twonumber
types.Removing the destructuring and using array accesses is equivalent but produces no error:
Playground Link: link
Related Issues:
The text was updated successfully, but these errors were encountered: