Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,11 @@ export function {{classname}}FromJSONTyped(json: any, ignoreDiscriminator: boole
if (Array.isArray(json)) {
if (json.every(item => typeof item === 'object')) {
{{/-first}}
if (json.every(item => instanceOf{{{.}}}(item))) {
return json.map(value => {{{.}}}FromJSONTyped(value, true));
if (json.every(item => instanceOf{{{.}}}(item as object))) {
return json.map(item => {{{.}}}FromJSONTyped(item, true));
}
{{#-last}}
}
return json;
}
{{/-last}}
{{/oneOfArrays}}
Expand All @@ -70,15 +69,15 @@ export function {{classname}}FromJSONTyped(json: any, ignoreDiscriminator: boole
{{#items}}
{{#isDateType}}
if (Array.isArray(json)) {
if (json.every(item => !(isNaN(new Date(json).getTime()))) {
return json.map(value => new Date(json);
if (json.every(item => !(isNaN(new Date(item).getTime())))) {
return json.map(item => new Date(item));
}
}
{{/isDateType}}
{{#isDateTimeType}}
if (Array.isArray(json)) {
if (json.every(item => !(isNaN(new Date(json).getTime()))) {
return json.map(value => new Date(json);
if (json.every(item => !(isNaN(new Date(item).getTime())))) {
return json.map(item => new Date(item));
}
}
{{/isDateTimeType}}
Expand Down Expand Up @@ -161,12 +160,11 @@ export function {{classname}}ToJSONTyped(value?: {{classname}} | null, ignoreDis
if (Array.isArray(value)) {
if (value.every(item => typeof item === 'object')) {
{{/-first}}
if (value.every(item => instanceOf{{{.}}}(item))) {
return value.map(value => {{{.}}}ToJSON(value as {{{.}}}));
if (value.every(item => instanceOf{{{.}}}(item as object))) {
return value.map(item => {{{.}}}ToJSON(item as {{{.}}}));
}
{{#-last}}
}
return value;
}
{{/-last}}
{{/oneOfArrays}}
Expand All @@ -175,15 +173,15 @@ export function {{classname}}ToJSONTyped(value?: {{classname}} | null, ignoreDis
{{#items}}
{{#isDateType}}
if (Array.isArray(value)) {
if (value.every(item => item instanceof Date) {
return value.map(value => value.toISOString().substring(0,10)));
if (value.every(item => item instanceof Date)) {
return value.map(item => item.toISOString().substring(0,10));
}
}
{{/isDateType}}
{{#isDateTimeType}}
if (Array.isArray(value)) {
if (value.every(item => item instanceof Date) {
return value.map(value => value.toISOString();
if (value.every(item => item instanceof Date)) {
return value.map(item => item.toISOString());
}
}
{{/isDateTimeType}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,13 @@ export function TestArrayResponseFromJSONTyped(json: any, ignoreDiscriminator: b
}
if (Array.isArray(json)) {
if (json.every(item => typeof item === 'object')) {
if (json.every(item => instanceOfTestA(item))) {
return json.map(value => TestAFromJSONTyped(value, true));
if (json.every(item => instanceOfTestA(item as object))) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using item as object we could also tell typescript that json is object[] if the condition that checks that every item is object is true:

if (json.every((item): item is object => typeof item === 'object')) {
    if (json.every(item => instanceOfTestA(item))) {
      // ...
    }
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another thing: The check, that every item in the array is an object should also check that the item is not null, because typeof null also returns 'object'.

if (json.every((item): item is object => item !== null && typeof item === 'object')) {
// ...
}

Otherwise instanceOfTestA will be called with null if json contains [null], and that will cause a TypeError because the in operator cannot be used with null.

export function instanceOfTestA(value: object): value is TestA {
    if (!('foo' in value) || value['foo'] === undefined) return false;
    return true;
}

return json.map(item => TestAFromJSONTyped(item, true));
}
if (json.every(item => instanceOfTestB(item))) {
return json.map(value => TestBFromJSONTyped(value, true));
if (json.every(item => instanceOfTestB(item as object))) {
return json.map(item => TestBFromJSONTyped(item, true));
}
}
return json;
}
if (Array.isArray(json)) {
if (json.every(item => typeof item === 'string')) {
Expand All @@ -71,14 +70,13 @@ export function TestArrayResponseToJSONTyped(value?: TestArrayResponse | null, i
}
if (Array.isArray(value)) {
if (value.every(item => typeof item === 'object')) {
if (value.every(item => instanceOfTestA(item))) {
return value.map(value => TestAToJSON(value as TestA));
if (value.every(item => instanceOfTestA(item as object))) {
return value.map(item => TestAToJSON(item as TestA));
}
if (value.every(item => instanceOfTestB(item))) {
return value.map(value => TestBToJSON(value as TestB));
if (value.every(item => instanceOfTestB(item as object))) {
return value.map(item => TestBToJSON(item as TestB));
}
}
return value;
}
if (Array.isArray(value)) {
if (value.every(item => typeof item === 'string')) {
Expand Down
Loading