json-typescript-mapper
json-typescript-mapper copied to clipboard
error TS2345
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 () => {}'.
try this. using<any>
before the Class type
deserialize(<any>myType,jsonObject)
I got the same error, also when using <any>
.
I use the "strictNullChecks": true
in tsconfig.json.
It's definitely the strictNullChecks: true
that breaks it
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.