SergioP
SergioP

Reputation: 608

Enums with Typescript

I have this apparently simple portion of code with enums.

These are the types definition:

export enum AcquisitionStatus {
  IDLE, RUNNING, DONE, ERROR,
}

export type State = {
  acquisitionCycleStatus: {
    progress: number
    status: keyof typeof AcquisitionStatus
  }
}

This is the function where I'm using them:

const initialState = {
  acquisitionCycleStatus: {
    progress: 0,
    status: AcquisitionStatus.IDLE,
  },
}

const StoreContext = createContext<[State, Dispatch<Actions>]>([
  initialState,
  () => null,
])

This is the error is reporting Typescript on initialState.

Type ‘{ acquisitionCycleStatus: { progress: number; status: AcquisitionStatus; }; }’ is not assignable to type ‘State’.
  The types of ‘acquisitionCycleStatus.status’ are incompatible between these types.
    Type ‘AcquisitionStatus’ is not assignable to type ‘“IDLE” | “RUNNING” | “DONE” | “ERROR”’.

What I'm doing wrong? Could someone help me? Thanks.

Upvotes: 0

Views: 136

Answers (1)

Shahar Amir
Shahar Amir

Reputation: 75

the enums gets values like 0,1,2,3 you need to tell the enums thier values.

try

export enum AcquisitionStatus {
  IDLE = 'IDLE',
  RUNNING = 'RUNNING',
  DONE = 'DONE',
  ERROR = 'ERROR',
}

Upvotes: 1

Related Questions