A
A
Andrey Okhotnikov2020-02-20 09:53:47
typescript
Andrey Okhotnikov, 2020-02-20 09:53:47

Property does not exist on type {}?

When initializing the function, there is an empty object user = {}
Then I get the user and assign it to a variable.
Further, when I try to access its fields, for example user.name, I get an error ts -

Property 'name' does not exist on type {}

How can I get rid of this other than setting all fields initially as name: ''?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Aetae, 2020-02-20
@tsepen

interface User {
  name?: string;
}

let user: User = {};
or
interface User {
  name: string;
}

let user = {} as User;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question