TypeScript has no trouble letting me assign the myValue field on objects of this type, but I couldn't figure out to directly instantiate one of these objects and assign myValue at the same time.
interface FooInterface {
myValue: number;
}
class FooClass implements FooInterface {
myValue: number;
}
declare type ShortHandEqualType = string | number | boolean | Date;
declare type KeyOf<MT extends object> = Extract<keyof MT, string>;
type Bar<MT extends object> = {
[P in KeyOf<MT>]?: (MT[P] & ShortHandEqualType);
}
function run<T extends FooInterface>(item: Bar<T>) {
// Success
item.myValue = 1;
// Success
const x: Bar<T> = {myValue: undefined};
x.myValue = 2;
// Success
const y: Bar<T> = {};
y.myValue = 3;
// Error: Type '{ myValue: 4; }' is not assignable to type 'Bar<T>'
const z: Bar<T> = {myValue: 4};
}
I tried making the field non-optional, but that didn't help.