title: How to do Typesafe JSON with fields of Enum type?
body: <p>I have some JSON files that conform to TypeScript interfaces that I have defined.</p>
  
  <p>For the most part, when importing such JSON files and assigning them to typed variables, TypeScript is able to infer the type signature automatically (see <code>behaves exactly as I want</code> in the code below). However when the type includes <strong>string enums</strong>, it no longer works (see <code>DOES NOT behave</code>). </p>
  
  <p>The following is a working <a href="https://stackoverflow.com/help/minimal-reproducible-example">Minimal, Reproducible Example</a>:</p>
  
  <p><strong>valid.json</strong></p>
  
  <pre><code>{ "id": 3.14159 }
  </code></pre>
  
  <p><strong>invalid.json</strong></p>
  
  <pre><code>{ "id": "3.14159" }
  </code></pre>
  
  <p><strong>validEnum.json</strong></p>
  
  <pre><code>{ "color": "red" }
  </code></pre>
  
  <p><strong>invalidEnum.json</strong></p>
  
  <pre><code>{ "color": "chartreuse" }
  </code></pre>
  
  <p><strong>index.ts</strong></p>
  
  <pre><code>import validJson from './valid.json'
  import invalidJson from './invalid.json'
  import validJsonEnum from './validEnum.json'
  import invalidJsonEnum from './invalidEnum.json'
  
  type ColorType = 'red' | 'blue' | 'yellow'
  
  type IJsonType = {"id": number}
  type IJsonTypeWithEnum = {"color": ColorType}
  
  // behaves exactly as I want
  const a: IJsonType = validJson    // no error
  const b: IJsonType = invalidJson  // ERROR: Type 'string' is not assignable to type 'number'.
  
  // DOES NOT behave as I want: SHOULD NOT error
  const c: IJsonTypeWithEnum = validJsonEnum    // ERROR: Type 'string' is not assignable to type 'ColorType'.
  
  // DOES NOT behave as I want: error should be that "chartreuse" is not assignable to type 'ColorType'
  const d: IJsonTypeWithEnum = invalidJsonEnum  // ERROR: Type 'string' is not assignable to type 'ColorType'.
  </code></pre>
  
  <p>I can make the error go away with <code>type IJsonTypeWithEnum = {"color": string}</code>, but that defeats the purpose.</p>
  
  <p>Is there any work-around or a compiler switch to make TypeScript recognize enum values inside JSON as strings? Or is it a dead-end TypeScript limitation for JSON type inference?</p>