I've encountered a situation where I need to create a generic function that accepts an object as a parameter, but with the condition that the object cannot have properties in the format __property__
. Currently, my approach is:
type RemoveProps = {
[K in keyof T as K extends `__${infer _K}__` ? never : K]: T[K];
}
function doSomething<T extends {}>(arg: RemoveProps<T>):
{
// ...
}
Although this works fine, it doesn't prevent scenarios like this:
doSomething<{ __not_allowed__: any }>(arg);
Is there a way to make TypeScript throw an error if the generic type contains these prohibited properties?