I am having a problem with Types in the backend of my application. The application has a data of various patients for doctor to manage. I am trying to add a new entry into an existing patient and I am getting this error:
Argument of type '{ type: "Hospital" | "OccupationalHealthcare" | "HealthCheck"; description: string; date: string; specialist: string; diagnosisCodes?: string[] | undefined; id: number; }' is not assignable to parameter of type 'Entry'.
Type '{ type: "Hospital" | "OccupationalHealthcare" | "HealthCheck"; description: string; date: string; specialist: string; diagnosisCodes?: string[] | undefined; id: number; }' is not assignable to type 'HealthCheckEntry'.
Types of property 'type' are incompatible.
Type '"Hospital" | "OccupationalHealthcare" | "HealthCheck"' is not assignable to type '"HealthCheck"'.
Type '"Hospital"' is not assignable to type '"HealthCheck"'.ts(2345)
I checked everything and it seems like the code should be working. Can anyone help me with this? The error is on this row: patientData.find(p => p.id === patientId)?.entries.push(newEntry);
import patientData from '../../data/patients'
import { Patient, NonSensitivePatientData, NewPatient, NewEntry } from '../types';
const patients: Array<Patient> = patientData
const getPatients = (): Patient[] => {
return patients;
};
const addEntry = (entry: NewEntry, patientId: string): Patient | undefined => {
const newEntry = {
id: 1,
...(entry as NewEntry)
}
patientData.find(p => p.id === patientId)?.entries.push(newEntry);
const patient = patients.find(p => p.id === patientId);
console.log(newEntry);
return patient;
}
export default {
addEntry
};
Here are my types:
interface BaseEntry {
id: string;
description: string;
date: string;
specialist: string;
diagnosisCodes?: Array<Diagnose['code']>;
}
export enum HealthCheckRating {
"Healthy" = 0,
"LowRisk" = 1,
"HighRisk" = 2,
"CriticalRisk" = 3
}
export interface HealthCheckEntry extends BaseEntry {
type: "HealthCheck";
healthCheckRating?: HealthCheckRating;
}
interface Discharge {
date: string;
criteria: string;
}
export interface HospitalEntry extends BaseEntry {
type: "Hospital";
discharge: Discharge;
}
interface SickLeave {
startDate: string;
endDate: string;
}
export interface OccupationalHealthcareEntry extends BaseEntry {
type: "OccupationalHealthcare";
employerName: string;
sickLeave?: SickLeave;
}
export type Entry =
| HospitalEntry
| OccupationalHealthcareEntry
| HealthCheckEntry;
export interface Patient {
id: string;
name: string;
dateOfBirth: string;
ssn: string;
gender: Gender;
occupation: string;
entries: Entry[];
}
export type NewEntry = Omit<Entry, 'id'>;