How can I create a function that takes a configuration object and returns a processed version with the same keys but different values? This function should:
- Offer suggestions for the properties of the configuration object when calling the function.
- Inform the compiler about the specific properties that are returned by the function.
.
type Config = {
a?: number
b?: string
}
function processConfig(x: Config): Config {
return x
}
function processConfigExtended<T extends Config>(x: T): T {
return x
}
In this scenario, the processConfig() function meets the first requirement while the processConfigExtended() function fulfills the second:
processConfig({a: 1}).<CTRL+SPACE> // Indicates the resulting type as "{ a?: number, b?: string }", but I desire it to be "{ a: number }".
processConfigExtended({<CTRL+SPACE> // Does not provide suggestions for the Config type.
Is there a method to design a function that fulfills both requirements simultaneously?