0

Say one has following JSON file…

{
  "hardened-computer": {
    "metadata": {
      "title": "How to verify integrity of release?"
    },
    "content": "foo"
  },
  "release-integrity": {
    "metadata": {
      "title": "What is a hardened computer?"
    },
    "content": "bar"
  }
}

…and one imports file in TypeScript project which uses part of URI as property key (example: /faq/[topic]).

How can one infer object property type SomeType?

import faq from "./faq.json"

const topic = getTopic() // returns hardened-computer, release-integrity or any other topic from URI

let topicData: SomeType

…

topicData = faq[topic as keyof typeof faq]
sunknudsen
  • 6,356
  • 3
  • 39
  • 76

1 Answers1

0

Figured it out thanks to https://stackoverflow.com/a/72463958/4579271.

let topicData: typeof faq[keyof typeof faq]
sunknudsen
  • 6,356
  • 3
  • 39
  • 76