We use as const
to narrow the types:
[ {foo: 'foo' }, { bar: 'bar' } ] as const
but this also adds readonly
modifiers for all props and values. As of const
this makes sens, but I would like to have the narrowing, without the readonly
modifiers, as offen you want to modify the objects in tuple, or just pass it further to some method, which is of the same interface, but without deep readonly
let foo = [ {foo: 'foo' }, { bar: 'bar' } ]
here the foo is of type:
({
foo: string;
bar?: undefined;
} | {
bar: string;
foo?: undefined;
})[]
but I would expect to get
[{
foo: string;
}, {
bar: string;
}]
There for we use as const
, but in most cases it is not possible, as foo
/bar
get deeply nested readonly
modifiers, we could use something like
type DeepWritable<T> = { -readonly [P in keyof T]: DeepWritable<T[P]> };
But it would be wired to use as const
which adds readonly
, and then DeepWritable
to remove it.
Do you know any alternative to as const
to narrow the tuple type without having manually to define types.