Nswag: Typescript 2.7 - Incompatible with `strictPropertyInitialization`

Created on 11 Feb 2018  路  4Comments  路  Source: RicoSuter/NSwag

Versions

NSwag.MSBuild: 11.15.0
NSwag.AspNetCore: 11.15.0
Microsoft.AspNetCore: 2.0.1

Issue

When I build my Typescript 2.7 code with strict on, I'm getting:
TS2564: Property 'x' has no initializer and is not definitely assigned in the constructor.
Note that this is a newer feature of Typescript 2.7 called Strict Class Initialization

Example nswag.json:

{
  "runtime": "WinX86",
  "swaggerGenerator": {
    "webApiToSwagger": {
      "controllerNames": [
        "y1.x1Controller",
        "y2.x2Controller"
      ],
      "isAspNetCore": true,
      "defaultUrlTemplate": "api/{controller}/{id?}",
      "defaultPropertyNameHandling": "CamelCase",
      "defaultReferenceTypeNullHandling": "Null",
      "defaultEnumHandling": "Integer",
      "flattenInheritanceHierarchy": false,
      "generateKnownTypes": true,
      "generateXmlObjects": false,
      "generateAbstractProperties": false,
      "addMissingPathParameters": false,
      "ignoreObsoleteProperties": false,
      "allowReferencesWithProperties": false,
      "output": null,
      "assemblyPaths": [
        "../bin/Debug/net471/x.exe"
      ],
      "referencePaths": []
    }
  },
  "codeGenerators": {
    "swaggerToTypeScriptClient": {
      "className": "{controller}Client",
      "moduleName": "",
      "namespace": "",
      "typeScriptVersion": 2.0,
      "template": "Fetch",
      "promiseType": "Promise",
      "httpClass": "Http",
      "injectionTokenType": "OpaqueToken",
      "dateTimeType": "Date",
      "nullValue": "Undefined",
      "generateClientClasses": true,
      "generateClientInterfaces": false,
      "generateOptionalParameters": false,
      "wrapDtoExceptions": false,
      "wrapResponses": false,
      "generateResponseClasses": true,
      "responseClass": "SwaggerResponse",
      "useTransformOptionsMethod": false,
      "useTransformResultMethod": false,
      "generateDtoTypes": true,
      "operationGenerationMode": "MultipleClientsFromOperationId",
      "markOptionalProperties": true,
      "generateCloneMethod": false,
      "typeStyle": "Class",
      "generateDefaultValues": true,
      "excludedTypeNames": [],
      "handleReferences": false,
      "generateConstructorInterface": true,
      "convertConstructorInterfaceData": false,
      "importRequiredTypes": true,
      "useGetBaseUrlMethod": false,
      "baseUrlTokenName": "API_BASE_URL",
      "queryNullValue": "",
      "serviceHost": "",
      "output": "index.ts"
    }
  }
}

Work around

Switching strictPropertyInitialization to false resolves these errors for now.


Please let me know if I missed any relevant details. Thank you

Most helpful comment

v11.15.1

All 4 comments

Maybe we could just generate "definite assignment assertion modifiers" (i.e. "!")?

I hand-edited my index.ts to add those definite assignment assertions as described and can confirm that everything works as expected on my end. I believe that would solve the issue.

Note I only added them on the ones that were being complained about (e.g. the properties where they don't have ? at the end, suggesting that assignment isn't required by them)

The problem is that adding ! only satisfies the compiler... you can end up with uninitialized objects (with undefined props). It would be better to assign default values instead !

v11.15.1

Was this page helpful?
0 / 5 - 0 ratings