简体   繁体   中英

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.

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',
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM