-1

I'm using NSwag to create a TypeScript client for an existing service.

It generates TypeScript that looks like this (example below is a simplified version to highlight the issue):

export interface IModelBase { }

export class ModelBase implements IModelBase {
    constructor(data?: IModelBase) {
        if (data) {
            for (var property in data) {
                if (data.hasOwnProperty(property))
                    (<any>this)[property] = (<any>data)[property];
            }
        }
    }
}
export interface IDerived {
    name?: string | undefined;
}

export class Derived extends ModelBase implements IDerived {
    name?: string | undefined;

    constructor(data?: IDerived) {
        super(data);
    }
}

When I attempt to instantiate this class with:

let d = new Derived({name: "Bob"});
alert(d.name);// Shows undefined

This appears to be happening because when TypeScript transpiles this to JavaScript, it generates something like this:

class ModelBase {
  constructor(data) {
    if (data) {
      for (var property in data) {
        if (data.hasOwnProperty(property)) this[property] = data[property];
      }
    }
  }
}
class Derived extends ModelBase {
  constructor(data) {
    super(data);
    this.name = void 0;//!!!Why is this here?
  }
}

I'm guessing that TypeScript can't "see" the assignment of the name field because it's hidden in the ModelBase constructor in the for(property in data) loop so it's trying to ensure that the field is initialized to undefined. Unfortunately this occurs after the call to super(...) which presumably did assign a value

When I put the example above into https://www.typescriptlang.org/play I haven't been able to find any combination of settings that causes this, so where is it coming from?

I am using this typescript within a create-react-app I'm not sure how to figure out if this codegen is occurring in TypeScript or somewhere else.

Typescript Version 5.2.2

I would expect to be able to create an initialize a TypeScript class defined by NSwag as recommended in: https://github.com/RicoSuter/NSwag/discussions/4044

  • I don't know how it literally works but I'm pretty sure hasOwnProperty will return false for any string since it is in the constructor of the base class. – ertucode Aug 29 '23 at 19:25
  • Without a [mre] I'm not sure how we can advise. I think you'll need to give more details about your environment if the Playground doesn't show it ... are you *sure* there's `this.name = void 0` and not some kind of `Object.defineProperty()`? I assume you're not just paraphrasing like that, but I want to be sure before I go too far down a rabbit hole – jcalz Aug 29 '23 at 19:25
  • It's pretty easy. `name` is defined as `string | undefined` in the derived class. And to prevent an error about uninitialized variables, the generated code assigns the only value that's possible in that situation, ie `undefined`. – derpirscher Aug 29 '23 at 19:33
  • Does this answer your question? [What is the point of void operator in JavaScript?](https://stackoverflow.com/questions/666936/what-is-the-point-of-void-operator-in-javascript) – user3840170 Aug 29 '23 at 19:41

0 Answers0