I am currently working on creating a Type Guard to prevent TypeScript from throwing an error on the final line, where I attempt to retrieve data based on a specific key. TypeScript is still identifying the environment variable as a string rather than a recognized key of the object. As a result, it is throwing the following error:
No index signature with a parameter of type 'string' was found on type...
.
Is there a possibility that I am overlooking a scenario where the environment variable may still be undefined as a key?
import JsonData from '../data/data.json'
const doesKeyExist: (
input: string | undefined
) => boolean = (input) =>
input && JsonData.hasOwnProperty(input)
if (!doesKeyExist(process.env.SOME_VARIABLE))
throw Error('Environment Variable not declared!')
const data = JsonData[process.env.NEXT_PUBLIC_TENANT_ID]