-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
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
[Feature request] Union type operator that prefer narrow types over wide ones #46548
Comments
I'm confused about the problem with the angular-style example, though. As far as I can tell, the only reason that the error goes unnoticed is that both I guess that |
I can't do either of these.
Handling
|
Why is "|&" rather than any other symbol? Thats because on provider side (i.e. variable declaration) this behaves like "&" or more close to function overloads sorted from most narrow to most wide (tryes to assign to more narrow type, if fails, go to the next more wide one, Just like for function overloads). While on consumer side more like "|" forcing to implement runtime typecheck in favor of some side of this composit type |
Hm, It seems I have found analogue to this feature in |
Use case which I have mentioned above highlights, that tagged unions are useful to provide typesafe configuration and express dependency of one field over another |
Suggestion
π Search Terms
narrow, types, type, wide. narrowing
β Viability Checklist
My suggestion meets these guidelines:
β Suggestion
When
|
type operator is used to alternate types sometimes it is useful alternate its behaviour i.e. prefer more narrow type than more wide (to better understand look at example below). Something likeconst
but not so strict. This type of behaviour will require to implement handilg for both sides of expression.π Motivating Example
This feature will help to better reflect dependency of one field on another (when both field are optional, but if one specified, other should be too).
π» Use Cases
Lets imagine (btw it is real world example) that we want autoconfigure navbar according to config of web app router. So when
navTitle
property is there and containg a string we are intrested in that element i.e. it will be placed on navbar with lable contained innavTitle
property. And it shoud lead to somewhere, right? But to where? To figure it out we should know path. ThusnavTitile
property is dependent onpath
(which is optional by default in angular). OK! That is not a difficult issue:Solution looks pretty good. Unless you are figurring out, that we should place all additional data into
data
property (this is angular way). Lets try:Why? That why!
Route | RouteWithNavTitle
will always resolve toRoute
sinceRouteWithNavTitle
is narrowedRoute
(remeberdata
property? in route it isany
but inRouteWithNavTitle
it is narrowed to{navTitile: string}
). Typescript will always prefer wider types for compatibility reasons. It is not always convinent as in considired example.You may argue: "there is an
const
expressions". Yes. But they are too strict.To solve the preoblem i suggest
|&
operator which is more like regular|
but will prefer narrow types when possible (when shape of object satisfy narrowed variant i.e. it can be assigned to more narrow variant). It will require to implement handling of both sides of expression (in this example forRoute
andRouteWithNavTitle
).P.S. Thank you very much for reading this. Leave your comments for me to understand if i missed something or explained wrong.
The text was updated successfully, but these errors were encountered: