json-typescript-mapper icon indicating copy to clipboard operation
json-typescript-mapper copied to clipboard

error TS2345

Open tfsjohan opened this issue 7 years ago • 4 comments

I get this error when running code using the deserialize method. Why?

(133,48): error TS2345: Argument of type '(new () => {}) | undefined' is not assignable to parameter of type 'new () => {}'. Type 'undefined' is not assignable to type 'new () => {}'.

tfsjohan avatar Jan 14 '18 19:01 tfsjohan

try this. using<any>before the Class type deserialize(<any>myType,jsonObject)

oalkabouss avatar Jan 16 '18 15:01 oalkabouss

I got the same error, also when using <any>. I use the "strictNullChecks": true in tsconfig.json.

NINI1988 avatar May 04 '18 09:05 NINI1988

It's definitely the strictNullChecks: true that breaks it

ali-habibzadeh avatar May 15 '18 17:05 ali-habibzadeh

I'm having the same problem caused by strictNullChecks: true when building Vue app using vue-cli. I get the following error stack when running the command vue-cli-service serve or vue-cli-service build:

133:48 Argument of type '(new () => {}) | undefined' is not assignable to parameter of type 'new () => {}'.
Type 'undefined' is not assignable to type 'new () => {}'.
131 |             if (innerJson && isArrayOrArrayClass(innerJson)) {
    132 |                 return innerJson.map(
  > 133 |                     (item: any) => deserialize(metadata.clazz, item)
        |                                                ^
    134 |                 );
    135 |             }
    136 |             return;

163:9 Type 'undefined' is not assignable to type 'T'.
if (hasAnyNullOrUndefined(Clazz, json)) {
  > 163 |         return void 0;
        |         ^
    164 |     }

170:9 Type 'undefined' is not assignable to type 'T'.
if (!isTargetType(json, 'object')) {
  > 170 |         return void 0;
        |         ^
    171 |     }

All the errors result from index.ts inside the npm package. Only happens when I actually try to import JsonProperty, serialize or deserialize.

I have configured tslint to exclude node_modules folder, but for some reason it parses just this package, so it might be some bug in TS also?

I can also confirm that setting strictNullChecks: false solves this problem, but it's definitely not an preferred workaround to use on the long-term.

coddo avatar Jun 27 '18 17:06 coddo