Is there a more streamlined way to dynamically add a question mark to a variable type in TypeScript, or is the approach of rewriting the type with a question mark the best way to achieve this? I'm looking to ensure that the original variables remain required in the type while also accommodating the addition of new variables.
For example, consider the following scenario:
interface Vars {
a: number
b: number
}
interface NewVars {
a?: number
b?: number
}
const createVars = () => {
let vars: Vars = {
a: 1,
b: 2
}
const get = () => vars
const set = (newVars: NewVars) => {
vars = {...vars, ...newVars}
}
return { get, set }
}