Inconsistent undefined vs. null handling
See original GitHub issueOptional values for
- return values and
- property values
are correctly configurable with the TypeScriptGeneratorSettings.NullHandling
setting.
Parameter values, however, are always optional with null
, never with undefined
.
There is no deeper reason for this, is there?
Issue Analytics
- State:
- Created 6 years ago
- Comments:10 (4 by maintainers)
Top Results From Across the Web
javascript - null and undefined inconsistent comparison
Converts the arguments to primitives (which null and undefined already are). Checks if the arguments are String s (which they are not).
Read more >`undefined` vs. `null` revisited
It indicates that a variable does not currently point to an object – for example, when it hasn't been initialized yet. In contrast,...
Read more >Null and undefined (Reference) - Prisma
Prisma Client differentiates between null and undefined : null is a value. undefined means do nothing.
Read more >Spreading undefined is inconsistent. : r/typescript
The important question about undefined/null or other primitives is whether using them in a spread is ever intentional.
Read more >null - JavaScript - MDN Web Docs - Mozilla
null is not an identifier for a property of the global object, like undefined can be. Instead, null expresses a lack of identification, ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Seems the default value of
Undefined
is troublesome withstrict
mode.I think the default should be made to be
string | null
for a string field.You get this typescript generated with the default of
Undefined
: (I think this is still the default?)This is for this C# class:
The problem is that with
strict
mode you cannot assignnull
tostring | undefined
. I also don’t want to use?
, andJSON.stringify()
doesn’t serialized fields withundefined
assigned to them.So I think the default should be
null
for sure.I managed to switch to using
nullValue: 'Null'
in my nswag file, and that now gives me the correct type. So I’m fine with this, just suggesting changing the default.I have wondered if having a third option of
Null | Undefined
would make any sense?, but I can do that withPartial<Address>
if I need to (which I may do if I use the generated entities elsewhere).eg. You can do this, and create sub-models and always be sure the names match the generated code.:
Typescript is amazing!
@simeyla : I don’t think NSwag is impacted because it doesn’t create object litterals, but instead serializes the function parameter.