[TypeScript] correct way to tell typescript a property does exist?

I have the following function;
function myfunc<T extends MyInterface>(item: T) {
if ("somekey" in item) console.log(item.someKey) // Error property does not exist
}
function myfunc<T extends MyInterface>(item: T) {
if ("somekey" in item) console.log(item.someKey) // Error property does not exist
}
Is there a proper way to inform typescript that inside of that if block, by definition, that property very much does exist? or is this a case where TS is painfully blind to reality?
4 Replies
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
WillsterJohnson
WillsterJohnsonOP2y ago
yup that'd be it... apparently I was using 4.7 and forgot to add ^ to the version in package.json, thanks!
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
WillsterJohnson
WillsterJohnsonOP2y ago
this is just a toy project, I'm kinda expecting it to break often anyway. I tend to only use the basics of TS as well so hopefully they won't fundamentally change the language any time soon
Want results from more Discord servers?
Add your server