I have encountered an issue while trying to reference an enum in typescript as the value of an object property.
Here is the code snippet:
types.ts
export enum BlockContentResolverTypes {
RAW_STRING_RESOLVER = 'raw::string::resolver',
RAW_NUMBER_RESOLVER = 'raw::number::resolver',
}
export interface BlockAnswerType {
id: string;
key: string;
value: boolean | string | number;
label: {
type: BlockContentResolverTypes
value: string;
};
}
The goal is to make the label.type
accept values from the BlockContentResolverTypes
enum such as raw::string::resolver
or raw::number::resolver
.
However, when importing a JSON object and assigning it the type of BlockAnswerType
, a type error occurs.
Below is the imported object:
data.json
{
"data": {
"id": "b608dbcd-f6c0-423d-9efd-c957af86a3b6",
"key": "yes",
"label": {
"type": "raw::string::resolver",
"value": "Yes!"
},
"value": true
}
}
The Typescript error message received is:
Type 'string' is not assignable to type 'BlockContentResolverTypes'.
Initially, I tried declaring all the enum values manually, but that did not resolve the error. Where am I going wrong and what would be the correct approach for this situation?