I defined a class User which has a private field:
class User {
private name: string = 'name'
hello(): void {
}
}
and a function uses the User type:
function testUser(user: User) {
}
Instead of creating an instance of User, I want to use an object to simulate it:
testUser({
name: 'new-name',
hello: () => {}
})
But it has a compilation error:
Argument of type '{ name: string; hello: () => void; }' is not assignable to parameter of type 'User'.
Property 'name' is private in type 'User' but not in type '{ name: string; hello: () => void; }'.(2345)
I can't understand:
- The
nameis private in classUser, why do we have to provide it in my mock object? - Is it possible to provide a
private 'name'in the mock object, or find a way not provide it without compilation error?