Comment

Peter Bengtsson

I had seen something like that before but didn't grok the connection. I updated the blog post to demo your utility function suggestion fully. Feels a bit better, to be honest.
Thanks! ...as always!

Parent comment

Gregor

Hey Peter! That's one of the annoying things to dance around in every new project. I've started bringing this method into every new codebase: ```ts export function isKeyOfObject( key: string | number | symbol, obj: T, ): key is keyof T { return key in obj; } ``` It's not a perfect solution though, since objects are open, or non-restrictive, in TS, meaning an object could be passed in with more (runtime) keys than those defined on its (compile-time) type. In that case the type would be wrong, as it would indicate its one of the (compile-time) keys (which is the smaller set). Now I do still like it better than `Record` (afaik the other version has identical semantics), since it does not make the object so permissive as to indicate that any string key would have some string value. All the best from your still-in-Berlin friend Gregor