TypeScript const assertion: Avoid type widening inside function

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… Read More TypeScript const assertion: Avoid type widening inside function

How to convince TypesSript you will return an Array with a property that is not null

I am working with the following types: type Fruits = { type: ‘banana’ | ‘peach’ | ‘kiwi’, price: null | string; } type FruitsWithNonNull = Pick<Fruits, ‘type’> & { price: NonNullable<Fruits[‘price’]> } const fruits:Fruits[] = [{type:’banana’, price:null}, {type:’peach’, price:null},{type:’peach’, price:’12’}] const filteredFruits:FruitsWithNonNull[] = fruits.filter(fruit => fruit.price !== null); I get this error from filteredFruits: Type… Read More How to convince TypesSript you will return an Array with a property that is not null