Can a custom type constraint be created to ensure that a type includes certain non-optional keys from an object, but not all keys? For instance:
class Bar {
key1: number
key2: Object
key3: <another type>
}
const Y = {
key1: 'foo'
key2: 'bar'
bar: // compilation error
}
In this scenario, Y
contains only a subset of keys present in Bar
, with compile-time checks preventing access to non-existent keys. Essentially, similar to Partial<Bar>
but unlike it, attempting to reference Y.key3
would result in a compilation error because key3
is not defined on Y
. Is this achievable?