1

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'>;

1 Answer 1

2

I have figured it out: I had to do "type assertion". Type "NewEntry" does not have an id property, but I am adding an id to "entry", this process creates newEntry variable. Now I had to change it's type from NewEntry to Entry, it happens like this:

patientData.find(p => p.id === patientId)?.entries.push(newEntry as Entry);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.