Closed
Description
TypeScript Version: 3.6.0-dev.20190726
Search Terms:
type inference
Code
declare function f(x: string | undefined): string;
declare function id<T>(x: T): T;
let state: undefined | string = undefined;
while (true) {
const x = id(f(state));
state = x;
}
// A *self-contained* demonstration of the problem follows...
// Test this by running `tsc` on the command-line, rather than through another build tool such as Gulp, Webpack, etc.
Expected behavior:
x: string
Actual behavior:
'x' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.
Related Issues:
Metadata
Metadata
Assignees
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
ahejlsberg commentedon Jul 28, 2019
This is a design limitation in the control flow analyzer. Ideally the analyzer would realize that
id(f(state))
always has typestring
regardless of the type ofstate
. However, the analyzer attempts to resolve the type of the expression, which entails resolving the type ofstate
, which entails resolving the type ofx
(because of thestate = x
assignment and the back edge of the loop), which leads to a circularity and therefore an implicitany
.