Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

327
Views
Typescript - Error when referencing enum in object property

I am trying to reference an enum in typescript as the value of an object property however I am getting an error when doing so

Here is the code:

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;
  };
}

What I am trying to do is say that the label.type can equal any of the values in the BlockContentResolverTypes enum ie raw::string::resolver or raw::number::resolver.

When I try and import a JSON object into my project and assign it a type of BlockAnswerType I get a type error.

Here is the object I am importing:

data.json

{
  "data": {
    "id": "b608dbcd-f6c0-423d-9efd-c957af86a3b6",
    "key": "yes",
    "label": {
      "type": "raw::string::resolver",
      "value": "Yes!"
    },
    "value": true
  }
}

And the Typescript error I get is:

Type 'string' is not assignable to type 'BlockContentResolverTypes'.

I initially thought the error meant I had to manually declare all the enum values like so:

export interface BlockAnswerType {
  id: string;
  key: string;
  value: boolean | string | number;
  label: {
    type: BlockContentResolverTypes.RAW_STRING_RESOLVER | BlockContentResolverTypes.RAW_NUMBER_RESOLVER
    value: string;
  };
}

But this still produces an error.

Where have I gone wrong and am I using the appropriate approach for what I am trying to achieve here?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You haven't explained how you're ingesting the the JSON and where it's being parsed, but when parsing the JSON string, just assert the type and the compiler will accept it.

You are, of course, responsible for ensuring that the JSON structure matches the type you have defined for it (and can do so at runtime if desired through validation of all of its properties after parsing).

TS Playground

const json = `{
  "data": {
    "id": "b608dbcd-f6c0-423d-9efd-c957af86a3b6",
    "key": "yes",
    "label": {
      "type": "raw::string::resolver",
      "value": "Yes!"
    },
    "value": true
  }
}`;

const blockAnswer = JSON.parse(json).data as BlockAnswerType;
blockAnswer.label.type // BlockContentResolverTypes
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!