I have the following object with a const assertion:
const foo = {
bar: ['a', 'b'],
} as const;
My goal is to write a function that updates the bar array and correctly infers the new type.
I can achieve the intended result when I pass foo.bar into the function:
type Bar = Readonly<string[]>;
function update<T extends Bar>(arr: T) {
return {
bar: [...arr, 'c'],
} as const;
}
const updatedFoo = update(foo.bar);
// Inferred type is as expected:
//
// const updatedFoo: {
// readonly bar: readonly ["a", "b", "c"];
// }
But I cannot get this to work when I pass in foo itself:
type Foo = Readonly<{ bar: Bar }>;
function update2<T extends Foo>(obj: T) {
return {
bar: [...obj.bar, 'c'],
} as const;
}
const updatedFoo2 = update2(foo);
// Inferred type is too wide:
//
// const updatedFoo2: {
// readonly bar: readonly [...string[], "c"];
// }
How can I rewrite update2 to correctly infer the type of bar as readonly ["a", "b", "c"]?