Reputation: 1538
I created type definitions for a JSON API object:
type CommonCurrentJob = {
id: number,
qty: 29,
qty_changed: 27,
created_at: string,
updated_at: string
}
type Job = {
user_id: number,
status: 'open' | 'closed',
location: 'string',
history: Array<{[number]: string}>,
note: string,
}
type JobDetails = Array<{iaj_id: number, code: number}>;
type CurrentJob = {
inventoryJob: Job & CommonCurrentJob,
inventoryJobDetails: JobDetails & CommonCurrentJob
}
In the fetch call I am performing a map on json.inventoryJobDeails:
return fetch(url, {credentials: 'include'})
.then((response) => {
return response.json();
})
.then((json: CurrentJob) => {
console.log(json);
const location = json.inventoryJob.location;
const ref_note = json.inventoryJob.note;
const id = json.inventoryJob.id;
const models = json.inventoryJobDetails.map((j) => {
return Object.assign({}, {
code: j.code,
qty: j.qty
})
});
this.setState({ currentCodes: models, location: location, ref_note: ref_note, id: id})
return json
})
The error Flow is giving me:
Error:(152, 32) Flow: call of method `map`. Method cannot be called on any member of intersection type intersection
I am new to Flow and do not understand why intersection types can't be mapped. Any help would be appreciated.
Upvotes: 1
Views: 60
Reputation: 1538
loganfsmyth has solved my issue in the comments:
JobDetails & CommonCurrentJob
is "a type that is an array" combined with "an object with properties", so it'd be an array with normal numeric indices that also has properties alongside the indexes.
So I have refactored my code to create a pure object intersection:
type CommonCurrentJob = {
id: number,
qty: 29,
qty_changed: 27,
created_at: string,
updated_at: string
}
type Job = {
user_id: number,
status: 'open' | 'closed',
location: 'string',
history: {[number]: string}[],
note: string,
id: number,
qty: 29,
qty_changed: 27,
created_at: string,
updated_at: string
} & CommonCurrentJob;
type JobDetails = {
iaj_id: number,
code: number,
id: number,
qty: 29,
qty_changed: 27,
created_at: string,
updated_at: string
} & CommonCurrentJob;
type CurrentJob = {
inventoryJob: Job,
inventoryJobDetails: JobDetails[]
}
Upvotes: 1