Is there a way to use arrays with object properties without using @ts-ignore in typescript
Lets say I have this function, it attempts to convert an array to an array with the prop property. This works fine except for the fact that their is a @ts-ignore
const addPropToArray = <T extends unknown>(x: T[]): T[] & {prop: number} => {
//@ts-ignore
const y: T[] & {prop: number} = [...x];
y.prop = 3;
return y
};
Obviously I can't do this
const x: string[] & {prop:number} = // what could go here
by itself which is why I was thinking of using the function but even then the function must have a @ts-ignore.
Are arrays with object properties just such a bad idea that typescript doesn't try to support them well, or is their something I'm missing?
Maybe their is some variation of this that could work?
const x: string[] & {prop:number} = {...["a","b","c"], prop: 4}
obviously the problem here is that it isn't an array any more.
EDIT:
I realize I could just cast the value instead of using @ts-ignore but that sill doesn't seem like the best solution
Object.assignwill work but a spread will not, by the way. It would look likeObject.assign([...x], {prop: 3})but I can’t check that for accuracy at the moment