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.

Inconsistent undefined vs. null handling

See original GitHub issue

Optional 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:open
  • Created 6 years ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
simeylacommented, Jan 25, 2019

Seems the default value of Undefined is troublesome with strict 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?)

export interface Address {
    firstName: string | undefined;
    lastName: string | undefined;

This is for this C# class:

public class Person
{
    [JsonProperty(Required = Required.AllowNull)]
    public string FirstName { get; set; }

    [JsonProperty(Required = Required.AllowNull)]
    public string LastName { get; set; }
}

The problem is that with strict mode you cannot assign null to string | undefined. I also don’t want to use ?, and JSON.stringify() doesn’t serialized fields with undefined 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.

“codeGenerators”: { “swaggerToTypeScriptClient”: { “className”: “{controller}Client”, “moduleName”: “API”, “namespace”: “”, “typeScriptVersion”: 2.7, “template”: “Angular”, “promiseType”: “Promise”, “httpClass”: “HttpClient”, “useSingletonProvider”: true, “injectionTokenType”: “InjectionToken”, “rxJsVersion”: 6.0, “dateTimeType”: “MomentJS”, “nullValue”: “Null”,

I have wondered if having a third option of Null | Undefined would make any sense?, but I can do that with Partial<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.:

 type UIAddress = Partial<Address>;
 type CityStateZip = Pick<Address, 'city' | 'state' | 'zip'>;

Typescript is amazing!

0reactions
jeremyVignellescommented, Jun 10, 2021

@simeyla : I don’t think NSwag is impacted because it doesn’t create object litterals, but instead serializes the function parameter.

Read more comments on GitHub >

github_iconTop 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 >

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