We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
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
Generic type narrowing
https://www.typescriptlang.org/play?ts=5.0.4#code/GYVwdgxgLglg9mABFApgZygHgCqJQD1TABM1EBDMATwG0BdRAH0QCUUI4AnYzDTmMAHMANBWoA+cQAo4AIwBWALkTYAlIgDeAWABQifYhjBEUgIKdO5KgDoYac5aoyF6xswDkAGxRCoAC3dDJDl5dW09A30QgG5dfQB6eMQAPQB+OIMAXzxPNBRNDMiYjMSU9Ij9TN0qnV1QSFgEZHQoACYcPEIfUjFaBmY2Dm5eKH4hUUoqSWclFTDCw2MzCytbexWnENV5ioNiiNK0heyUXPzwyKiFWIOko92amqA
function test<T extends any[] | Record<string, any>>(obj: T) { if (Array.isArray(obj) || 'length' in obj) { obj; // ^? (parameter) obj: T extends any[] | Record<string, any> } else { obj; // ^? (parameter) obj: never } } function test2<T extends any[] | Record<string, any>>(obj: T) { if (Array.isArray(obj)) { obj; // ^? (parameter) obj: T & any[] } else { obj; // ^? (parameter) obj: T extends any[] | Record<string, any> } }
In the else branch of the test function, obj is inferred as never type.
test
never
In the else branch of the test function, obj should be inferred as T extends any[] | Record<string, any> type.
T extends any[] | Record<string, any>
The text was updated successfully, but these errors were encountered:
isTypePresencePossible
ahejlsberg
Successfully merging a pull request may close this issue.
Uh oh!
There was an error while loading. Please reload this page.
Bug Report
π Search Terms
Generic type narrowing
π Version & Regression Information
β― Playground Link
https://www.typescriptlang.org/play?ts=5.0.4#code/GYVwdgxgLglg9mABFApgZygHgCqJQD1TABM1EBDMATwG0BdRAH0QCUUI4AnYzDTmMAHMANBWoA+cQAo4AIwBWALkTYAlIgDeAWABQifYhjBEUgIKdO5KgDoYac5aoyF6xswDkAGxRCoAC3dDJDl5dW09A30QgG5dfQB6eMQAPQB+OIMAXzxPNBRNDMiYjMSU9Ij9TN0qnV1QSFgEZHQoACYcPEIfUjFaBmY2Dm5eKH4hUUoqSWclFTDCw2MzCytbexWnENV5ioNiiNK0heyUXPzwyKiFWIOko92amqA
π» Code
π Actual behavior
In the else branch of the
test
function, obj is inferred asnever
type.π Expected behavior
In the else branch of the
test
function, obj should be inferred asT extends any[] | Record<string, any>
type.The text was updated successfully, but these errors were encountered: