I have below TypeScript type:
export type MHStatus = {
AVAILABLE: "AVAILABLE"
IN_PROGRESS: "IN_PROGRESS"
UNAVAILABLE: "UNAVAILABLE"
}
Now, I have one function which does below part:
const status = isTrue? MHStatus['AVAILABLE'] : MHStatus['UNAVAILABLE']
const formatOnj = await formatobjTOAdd(getterSetter, id, status)
Now I have below function:
export const formatobjTOAdd = (getterSetter: GetterSetter, requestId?: string, status?: MHStatus) => {
}
Here it throws an error says type cannot be used. Can anyone help me here?
2
The reason you get this error is that you are trying to use a type as a value, which doesn’t work because types do not exist at runtime.
If you want to keep the code mostly the same you can turn MHStatus
into an enum like this:
enum MHStatus {
AVAILABLE = "AVAILABLE",
IN_PROGRESS = "IN_PROGRESS",
UNAVAILABLE = "UNAVAILABLE"
}
Full working example (with missing details added):
enum MHStatus {
AVAILABLE = "AVAILABLE",
IN_PROGRESS = "IN_PROGRESS",
UNAVAILABLE = "UNAVAILABLE"
}
type GetterSetter = 'get' | 'set';
const isTrue: boolean = true;
const getterSetter = 'get';
const id = 'requestId';
const formatobjTOAdd = (getterSetter: GetterSetter, requestId?: string, status?: MHStatus) =>
{
}
const myStatus = isTrue? MHStatus['AVAILABLE'] : MHStatus['UNAVAILABLE']
const formatOnj = formatobjTOAdd(getterSetter, id, myStatus)
Playground
Trixty is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.