How to create an object based on an interface in TypeScript?
Hi everyone! I started learning TypeScript and ran into a problem. I have an interface describing a user:
```typescript
interface User {
id: number;
name: string;
email: string;
isActive: boolean;
}
```
I want to create an empty object of this type, and then fill its fields as needed. I'm trying to do this:
```typescript
const user: User = {};
```
But TypeScript complains: `Type '{}' is missing the following properties from type 'User': id, name, email, isActive`.
I tried writing `const user: User = {} as User;` — this works, but it's unsafe because the fields remain undefined.
How to properly create an object based on an interface? Is there a way to do this without explicitly listing all fields? Maybe there are some patterns or TypeScript tricks?
No answers yet.
Log in to leave an answer