Given class MyClass, why are private constructor fields required when implementing an object with the type MyClass?
They're private, so shouldn't they be ignored when implementing an object that conforms to the class?
Tiny Example
class MyClass {
constructor(
private privateString: string,
) {}
method1(): number {
return 1;
}
method2(): number {
return 2;
}
}
/*
Property 'privateString' is missing in type '{ method1: () => number; method2: () => number; }'
but required in type 'MyClass'.ts(2741)
*/
const Stub: MyClass = {
method1: () => 1,
method2: () => 2,
};
/*
Type '{ privateString: string; method1: () => number; method2: () => number; }'
is not assignable to type 'MyClass'.
Property 'privateString' is private in type 'MyClass'
but not in type '{ privateString: string; method1: () => number; method2: () => number; }'.ts(2322)
*/
const Stub: MyClass = {
privateString: '',
method1: () => 1,
method2: () => 2,
};
Tested on Typescript 4.4.3