π Search Terms
Type inference following a type narrowing fails when done within a loop.
π Version & Regression Information
This bug exists ever since TS 3.7 where TS asserts was introduced.
β― Playground Link
https://www.typescriptlang.org/play/?ts=5.8.3#code/KYDwDg9gTgLgBAMwK4DsDGMCWEVwIYDOBwsAwjgCaZY4A8AKgHwAUAUHHGpddigFxx6AGlYBKAYWKwCnbjVwBvdnEwI4zAIRcUVeaLhKOHGAAsoEAO5wUwKwFEo5qM1HKAvqw+sYATzDA4ADc8ABtMCno-YBkAXjgAcgBBeLgAHwSAIRTWVm0CeHy8GCQCAWCwiKjYhOSc5HR5OF9-AEkUBBJgdGAAMTxMMJQAczaCcOAAGQgIMBcDZUkSGHIdHhxmQuKZDRi4+KzXDgRodTz4AH04CDUAbQBGIQAmAF19QyMAeg+4AFUbcGAGGAFDgJCcAnimxKjxSmAAtmAwmhqCEfHATIQmlEEngUD4UgAjQF4EoBahwCgQaLWCDwDGBAJ4LH+fAoFC0oq8VkgzAyKDADr87ogqj8jCoq5QFSrMUwCWYXDUGSWRUoHihTAALxIADplBwznAoQRHnA4sa4Pr8EQlitdLwNjAitCzbsavFDnAPF56hguc1gG1BV00MAAAqSBVDADq1BMECQMCmMzm70WZDkDotOz2B2UhuNpvNTq2lo46eWmfWhdde2SrjcQA
π» Code
export function assertCondition<T>(
condition: T,
): asserts condition {
if (!condition) {
throw new Error()
}
}
type validTypes = 'A' | 'B'
const status: validTypes = 'A'
function typeInferenceFailingInsideLoop() {
assertCondition(status !== 'B')
for (const _ of [1,2]) {
// Unexpected error: 'status2' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.
const status2 = status
assertCondition(status2 === 'A')
}
}
function typeInferencePassingWithoutLoop() {
assertCondition(status !== 'B')
const status2 = status
assertCondition(status2 === 'A')
}
π Actual behavior
Error thrown within the function call typeInferenceFailingInsideLoop stating 'status2' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.
π Expected behavior
Ideally, no type error should be thrown here.
Additional information about the issue
Possible workarounds
-
Redeclare the type to help TS infer (Playground link) Interestingly, the type narrowing still works as intended.
-
Don't use generic (Playground link) For checking of truthy conditions, one would need to change this to a boolean condition instead.
π Search Terms
Type inference following a type narrowing fails when done within a loop.
π Version & Regression Information
This bug exists ever since TS 3.7 where TS asserts was introduced.
β― Playground Link
https://www.typescriptlang.org/play/?ts=5.8.3#code/KYDwDg9gTgLgBAMwK4DsDGMCWEVwIYDOBwsAwjgCaZY4A8AKgHwAUAUHHGpddigFxx6AGlYBKAYWKwCnbjVwBvdnEwI4zAIRcUVeaLhKOHGAAsoEAO5wUwKwFEo5qM1HKAvqw+sYATzDA4ADc8ABtMCno-YBkAXjgAcgBBeLgAHwSAIRTWVm0CeHy8GCQCAWCwiKjYhOSc5HR5OF9-AEkUBBJgdGAAMTxMMJQAczaCcOAAGQgIMBcDZUkSGHIdHhxmQuKZDRi4+KzXDgRodTz4AH04CDUAbQBGIQAmAF19QyMAeg+4AFUbcGAGGAFDgJCcAnimxKjxSmAAtmAwmhqCEfHATIQmlEEngUD4UgAjQF4EoBahwCgQaLWCDwDGBAJ4LH+fAoFC0oq8VkgzAyKDADr87ogqj8jCoq5QFSrMUwCWYXDUGSWRUoHihTAALxIADplBwznAoQRHnA4sa4Pr8EQlitdLwNjAitCzbsavFDnAPF56hguc1gG1BV00MAAAqSBVDADq1BMECQMCmMzm70WZDkDotOz2B2UhuNpvNTq2lo46eWmfWhdde2SrjcQA
π» Code
π Actual behavior
Error thrown within the function call
typeInferenceFailingInsideLoopstating'status2' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.π Expected behavior
Ideally, no type error should be thrown here.
Additional information about the issue
Possible workarounds
Redeclare the type to help TS infer (Playground link) Interestingly, the type narrowing still works as intended.
Don't use generic (Playground link) For checking of truthy conditions, one would need to change this to a boolean condition instead.