question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Form control value is null when initialized with undefined

See original GitHub issue

Which @angular/* package(s) are the source of the bug?

forms

Is this a regression?

No

Description

When you initialize a FormControl with an undefined value (initial state) the FormControl#value is null. The value is null instead of undefined to be short

Please provide a link to a minimal reproduction of the bug

https://stackblitz.com/edit/angular-ivy-x37dzv

Please provide the exception or error you saw

If you try to do test something like the test will fail


// control was initialized with undefined
expect(control.value).toBe(undefined) // <-- in fact, this is null

Please provide the environment you discovered this bug in (run ng version)

Angular CLI: 14.1.0
Node: 16.16.0
Package Manager: npm 8.15.1
OS: win32 x64

Angular: 14.1.0
... animations, cdk, cli, common, compiler, compiler-cli, core 
... forms, material, platform-browser, platform-browser-dynamic
... router

Package                         Version
---------------------------------------------------------      
@angular-devkit/architect       0.1401.0
@angular-devkit/build-angular   14.1.0
@angular-devkit/core            14.1.0
@angular-devkit/schematics      14.1.0
@angular/flex-layout            14.0.0-beta.40
@schematics/angular             14.1.0
rxjs                            7.5.6
typescript                      4.7.4

Anything else?

No response

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:2
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

5reactions
AndrewKushnircommented, Aug 3, 2022

@MikaStark thanks for reporting the issue. It’s a known limitation of the Forms APIs where null is used as a default value in FormControl (see here). Changing this behavior to drop null as the default value would result in a breaking change, see my comment here: https://github.com/angular/angular/issues/40608#issuecomment-769424323. To make it a non-breaking change, we can potentially think of some additional APIs to define what the default value should be (like new FormControl(undefined, {defaultValue: undefined}) on per-control basis or FormControl.useAsDefault(undefined) to set if globally). In any case, this would require extra design work to figure out how that would affect the rest of the Forms logic (there might be more places were the code relies on the null being the default value). We don’t have any ETA, so I’d recommend using a workaround for now.

Given the fact that this issue is reported periodically (#40608, #40978), I’m keeping it open so that we can collect more signal.

4reactions
jnizetcommented, Aug 4, 2022

@alxhub maybe I misunderstood what you meant, but regarding your first point, null is and should still be accepted as a value for a nonNullable control. This looks contradictory, but that’s only because it was decided to rename initialValueIsDefault to nonNullable, making things very confusing IMHO.

A nonNullable control can have a null value. nonNullable only means that the reset() method won’t set the value of the control to null, but will instead set it to its initial value (which can be null, BTW). There are many control types (such as an input of type number for example) which should be reset to their initial value when reset() is called, but which must accept null as their value, simply because the user can clear the input, or because we want the input to be initially empty.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular - undefined to null field conversion
The undefined property indicates that a variable has not been assigned a value. When you assign null as default value then since JavaScript...
Read more >
FormGroup
Tracks the value and validity state of a group of FormControl instances. ... FormControl('') });. Notice that c.value.one has type string|null|undefined .
Read more >
undefined - JavaScript - MDN Web Docs - Mozilla
The global undefined property represents the primitive value undefined. It is one of JavaScript's primitive types.
Read more >
Understanding null safety
The null value is an instance of the Null class, and Null has no “length” getter. ... the handy ?. null-aware operator, and...
Read more >
Difference between null and undefined in JavaScript
A null means the absence of a value. You assign a null to a variable with the intention that currently this variable does...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found