You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I think this is a feature request rather than a bug report, but I'm not sure... I know that contextual typing is currently applied selectively, and that that's intentional, but I couldn't find many issues discussing what the boundaries for applying contextual typing are intended to be. I also don't know what real-world code would've motivated the current rules, so it's possible that my suggestion here would be helpful for the example case below but be counterproductive overall.
Anyway, here's the code I'm dealing with (simplified):
In this code, I'd expect the Book.categories getter to type check, but it doesn't.
The expression being returned is assignable to Category[] if Category[] is used to contextually type it βΒ i.e., the code works if I add satisfies Category[] at the end of the returned expression, or if I do const x: Category[] = /* same expression */; return x;.
However, without contextual typing, TS infers an array type (i.e., { label: string; children: never[]; }[]) as the type for the outermost children property, whereas ParentCategory requires that be a tuple type, to enforce that the array is non-empty.
So, my suggestion/feature request would to apply contextual typing in (at least) the case above (if it's not already supposed to apply there) β but again, I'm not sure what downsides this might have for other code.
The text was updated successfully, but these errors were encountered:
Suggestion
π Search Terms
return type contextual typing
β Viability Checklist
My suggestion meets these guidelines:
β Suggestion + Motivating Example
I think this is a feature request rather than a bug report, but I'm not sure... I know that contextual typing is currently applied selectively, and that that's intentional, but I couldn't find many issues discussing what the boundaries for applying contextual typing are intended to be. I also don't know what real-world code would've motivated the current rules, so it's possible that my suggestion here would be helpful for the example case below but be counterproductive overall.
Anyway, here's the code I'm dealing with (simplified):
In this code, I'd expect the
Book.categories
getter to type check, but it doesn't.The expression being returned is assignable to
Category[]
ifCategory[]
is used to contextually type it βΒ i.e., the code works if I addsatisfies Category[]
at the end of the returned expression, or if I doconst x: Category[] = /* same expression */; return x;
.However, without contextual typing, TS infers an array type (i.e.,
{ label: string; children: never[]; }[]
) as the type for the outermostchildren
property, whereasParentCategory
requires that be a tuple type, to enforce that the array is non-empty.So, my suggestion/feature request would to apply contextual typing in (at least) the case above (if it's not already supposed to apply there) β but again, I'm not sure what downsides this might have for other code.
The text was updated successfully, but these errors were encountered: