Amy Blankenship
Sep 22, 2021

--

And yet. And yet...

After all the effort you spent making Typescript happy, Typescript still apparently didn't protect you from the error you're going to get when there isn't an item that has a property with that value in the array

function getBy<T, P extends keyof T>(model: T[], prop: P, value: T[P]): T | null {

return model.filter(item => item[prop] === value)[0] || null

}

What if, instead of wasting all that time learning how to make Typescript happy, you'd spent time learning about things more relevant to directly solving the business problem? Like, IDK, other methods on Array.prototype that would be more useful?

--

--

Amy Blankenship
Amy Blankenship

Written by Amy Blankenship

Full Stack developer at fintech company. I mainly write about React, Javascript, Typescript, and testing.

No responses yet