Closed
Conversation
If the narrowed type is assignable to the type parameter's *constraint*, then narrow to a type parameter with the constraint of the narrowed type.
Add test and baselines
Member
Author
|
As discussed with @yuit, the reason I narrow to function f<T extends Base>(x: T) {
return isDerived(x) && x; // f's return type appears to be `T extends Base`
}
function f2<T extends Base>(x: T): T {
return isDerived(x) && x;
// error: 'T' is not assignable 'T'; 'Derived' is not assignable to 'T'
}This should work by analogy with the non-generic version: function g(x: Base) {
return isDerived(x) && x; // g's return type is Derived
} |
Member
|
It seems like if one of your return type candidates is a type parameter, you don't want to return the narrowed type there. You just want to return the type parameter itself. I can't specifically think of other types that might have this characteristic. |
Contributor
|
This is already covered by #8010, closing. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #6445
If the narrowed type is assignable to the type parameter's constraint, then narrow to a type parameter with the constraint of the narrowed type. In the example in the bug,
After narrowing,
xandyhave the typeT extends Derivedinstead ofT extends Base.