Should i have separate types for the same data?


I’m creating my first react-native app with firebase / redux toolkit / typescript and I’m stuck on some points.

Atm I have this type

type Program = {
  id?: string
  name: string!
  trainings: Record<string, Training>
  userId: string

On my redux state I have this

export interface ProgramState {
  programs: Program[]
  currentEditingProgram: Program | null
  loading: boolean

const initialState: ProgramState = {
  programs: [],
  currentEditingProgram: null,
  loading: true,

My problem is that when i update with this function …

export const updateProgramById = createAsyncThunk(
  async (program: Program, thunkAPI) => {
      await updateDoc(doc(db,,!), program)

… i need to remove id property from {program} to save it on firestore as :



id:"",//Same as {id}

My questions are :

  1. Should i have separate type, one with id one without ?
  2. When I want to update one Program, should i save my currentEditingProgram object or just the id
    (I have a screen ProgramDetail and i can navigate to Training screen details)


You can have a separate type holding only data, without ID, and a generic type that combines those like this:

Typescript playgorund

//generic redux type with ID
type ReduxIdItem<T, IdType = string> = { id: IdType, data: T }

type ProgramData = {
  name: string
  trainings: Record<string, Training>
  userId: string

type ProgramRedux = ReduxIdItem<ProgramData>;

const test: ProgramRedux = {} as ProgramRedux;

//now you have the object separated from the id //hods the data without id // holds the id

Answered By – Svetoslav Petkov

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published