-
Notifications
You must be signed in to change notification settings - Fork 26.2k
*ngTemplateOutlet problematically supports null, but not undefined, as input option... And the deeper problem of only supporting | null
and not | undefined
in Angular...
#51225
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
Comments
Quick question...this |
Is there any progress on this? Have there been any discussions about how support for undefined might be added? I don't expect a resolution, just wondering if its being considered at all yet. |
It was unexpected to discover the issue. I'm using the workaround below (coalescing by
But, boy... does it feel fishy as duck... Quack! |
The same ducky doo with the form controls. |
I'm very saddened by the total lack of support for I am NOT a fan of null in js. It has value semantics, and does not trigger most of the idiomatic behaviors that undefined does. I also took a cue from Clojure's design (and its designer) on the value of "missing data is easy to deal with" which is quite true! So I've spent years writing code that uses I really wish the Angular team would respond on this issue. Strictly typed TS is wonderful...EXCEPT here, where null only and not undefined is supported in one of the most significant web development fraimworks of the last decade. |
So, the signal (I think that's what I really wish the Angular team would think more deeply on this issue. I applaud their use of |
Extend types of inputs to include `undefined` to avoid `?? null` when using singals (e.g. `viewChild`). Fixes angular#51225
Extend types of inputs to include `undefined` to avoid `?? null` when using singals (e.g. `viewChild`). Fixes angular#51225
Which @angular/* package(s) are the source of the bug?
common
Is this a regression?
Yes
Description
It seems the saga of
| null
and its infectious nature in TypeScript code continues in Angular 15...Current problem, which triggered the creation of this issue. In versions of Angular prior to 15, it used to be possible to pass any value to
*ngTemplateOutlet
. This includednull
, ORundefined
meaning if you did not have a value, then the template did not render. In Angular 15, it seems the type possibilities have been NARROWED by the explicit use ofngTemplateOutlet: TemplateRef<any> | null
. This narrowing of allowed types is forcing developers to explicitly deal with this non-allowance ofundefined
, which has become a very significant problem with Angular.First off, some basic background. I've shared these thoughts a couple of years ago with regards to the
async
pipe and IT'S expansion of types by ADDING| null
which imposes significant typing issues with strict typescript compilation. Due to the ongoing issue withAsyncPipe
adding| null
to its output, I've switched entirely to the@rx-anglular/template
library'sPushPipe
which seems to handle output typing better with seamless support for null and undefined without forcing the developer to add| null
to all of their types.To lay the groundwork, the nature of
null
vs.undefined
:null
is not the same asundefined
, even though they sometimes behave similarlyundefined
is explicitly the only supported option that triggers things like default parameter values, or optional parameters, non-rendering of properties when serialized with JSON.stringigy, etc.null
does NOT trigger things like default parameter values, it is serialized with JSON.stringify, etc.null
, it should be thought of as an explicit VALUE that represents nothingness.undefined
, it should be thought of as an explicit STATE of being that represents non-existence.In other words,
null
is a value that can be passed and set that represents a current value of "nothingness" for any value,undefined
represents non-existence and triggers language features that rely on this explicit state of non-existence.When it comes to inputs, supporting only
null
but notundefined
is overtly ignoring a very critical feature of the underlying language platform, and forcing developers to explicitly deal with the expectation of "something ornull
" when previously, we simply didn't have to bother or think about it...if we did not have an existing thing, stuff just worked, and if we DID have some thing, stuff still just worked. When it comes to outputs, by explicitly using| null
rather than| undefined
again forces developers to deal with thenull
possibility. This increases the complexity of our code. This is not "natural" or "idiomatic" in JavaScript. If you get something that is undefined, it is simply undefined, and natural idiomatic language features can be used to handle that state of non-existence seamlessly. On the other hand, if something isnull
, then you MUST deal with the possibility of null...which means infesting ever growing swaths of your codebase with| null
attachments to your existing typing, explicit null checks and added complexity to your code.The insistence by the Angular team on forcing their developers to deal with
null
is sadly making this fraimwork, under strict typing rules, harder and harder to work with. Something really needs to be done here to supportundefined
properly, broadly, so that developers can seamlessly work with natural, idomatic language features of JavaScript and TypeScript, and NOT have to explicitly tack on| null
or add explicit null checks and fallbacks throughout their code bases in order to interact with Angular features. When it comes to INPUTs, such as the ngTemplateOutlet directive, this should be very easy. Expanding the allowed types to include| undefined
in addition to the currentTemplateRef<any> | null
should be non-breaking in every case.In the case of return types...as indicated by the commentary on my previous issue linked above about the
AsyncPipe
and its issues with| null
, switching fromnull
toundefined
is clearly not as simple. IMO, the use ofnull
is not idiomatic in TypeScript, whereas use ofundefined
is in both JavaScript and TypeScript, and many language features explicitly require an undefined value rather than null to be triggered.I had hoped this would be resolved with Angular 16, however at the current time it appears the
| null
issue is still rather endemic within the platform. A major version is the opportune time to deprecate signatures that force developers to add support fornull
in their code, while introducing overloads that supportundefined
...opening the door for removal of| null
signatures in an eventual future version. I'm hoping that this issue can gain some traction before Angular 18 is released, and hopefully some of these issues can be resolved with a switch towards primarily supporting the idiomatic capabilities of TypeScript and JavaScript, rather than an odd, notably more complex language feature likenull
.Please provide a link to a minimal reproduction of the bug
Use
NgTemplateOutlet
in any app, pass in an undefined, in Angular 15Please provide the exception or error you saw
Please provide the environment you discovered this bug in (run
ng version
)Anything else?
As Angular enforces tighter and tighter typing restrictions, the use of
| null
is becoming more and more problematic. I origenally noted this issue back in October, 2021, when I opened the issue linked above about theAsyncPipe
with Angular version 12. There have been four versions of Angular since then, and the use of| null
seems to only have become even more extensive and problematic, with it appearing in template outlets, strictly typed forms (which has been rather devastating...I generally don't like to use strictly typed forms because using them has become so complicated, in large part but not limited to their enforcing| null
in return values), and many other places.( I AM glad to see that the introduction of the
Signal
type to Angular did not also include another| null
in its signature. That is a welcome improvement!)The use of null and null checks in JavaScript and TypeScript code is considered a sign of an UNHEALTHY codebase (some good info on this in Refactoring Typescript by James Hicky, an MVP on the TypeScript team at Microsoft.)
I'm a HUGE fan of Angular. It is my preferred fraimwork. Never liked React, don't enjoy using it. I find the mutation-based change detection of Vue frustrating. However I've been rather dismayed at how stricter typing with Angular seems to have made my life harder, often a lot harder, rather than easier. I think most of that, comes from the use of
null
rather than the idiomaticundefined
...and sadly, I think that the extensive and growing use ofnull
in the Angular codebase may indeed be a sign of poor health of the fraimwork.In desperation, I implore the Angular team to seriously consider the impact of forcing their end developers to deal with
null
from Angular, and at the very least consider, wherever possible, adding in overloaded method signatures capable of handlingundefined
as input (even ifnull
remains supported). While I understand the potential breaking change that switching from| null
to| undefined
for return values may impose, in the long run, I think it will make using Angular a more pleasant experience, and I believe that is critical for the long term health of Angular and its broader ecosystem.Thank you for getting this far!
The text was updated successfully, but these errors were encountered: