If we have data structured like this:
{
"key1": "hardcoded string",
"key2": "another hardcoded string",
}
Imagine a function with 2 parameters where the first parameter should refer to key1 and the second to its value.
For instance:
funcName('key1', 'hardcoded string')
The function operates as follows:
funcName(key, defaultValue) {
return jsonData[key] || defaultValue
}
To enable auto-suggest for the JSON's keys, we can do something like this:
import data from './data.json'
type JsonData = typeof data
funcName<K extends keyof JsonData>(key: K, defaultValue: JsonData[K]) {
return jsonData[key] || defaultValue
}
When attempting to pass the first parameter to funcName
, suggested options include key1, key2. However, if we choose key1, we want the second parameter to suggest only 'hardcoded string'. Currently, the code provides type suggestions only for the first parameter but not the second.
Does anyone have any ideas on how to link the second parameter suggestion to the value of the first parameter?