You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TypeScript中用户数据及对象数组赋值给User类失败问题排查

Fixing TypeScript Assignment Issues with User/Address Classes

Hey there, let's tackle your two questions and get this TypeScript code working properly!

1. Why are you getting an error when assigning user data to the User object?

The root issue here is that you're trying to assign plain JavaScript objects (like {name: 'Naveen', address: [...]}) directly to a variable typed as User[].

In TypeScript, when a class has private members (like private name in your User class or private city in Address), the type system treats these classes as more than just their structure. Only actual instances created with new User(...) or new Address(...) will be recognized as matching the User or Address type—plain objects that look the same won't cut it, because the private properties add a "type identity" that plain objects don't have.

2. How to fix the array assignment issue?

There are a few ways to resolve this, depending on whether you need full class functionality or just a data structure:

Option 1: Use Interfaces (Best for Data-Only Structures)

If you don't need the class methods/setters/getters and just want to define the shape of your data, interfaces are the way to go. They're designed exactly for this scenario:

interface Address {
  city: string;
}

interface User {
  name: string;
  address: Address[];
}

// This works perfectly now
let users: User[] = [{name: 'Naveen', address: [{'city': 'Bangalore'}]}];

Option 2: Create Actual Class Instances

If you need to keep the User and Address classes (for encapsulation, methods, etc.), you need to instantiate them instead of using plain objects:

class User {
  private name: string;
  private address: Address[];
  constructor(name: string, address: Address[]) {
    this.name = name;
    this.address = address;
  }
  public set _name(name: string) {
    this.name = name;
  }
  public get _name() {
    return this.name;
  }
}

class Address {
  private city: string;
  constructor(city: string) {
    this.city = city;
  }
  public set _city(city: string) {
    this.city = city;
  }
  public get _city() {
    return this.city;
  }
}

// Create Address and User instances explicitly
const bangaloreAddress = new Address('Bangalore');
const users: User[] = [new User('Naveen', [bangaloreAddress])];

Option 3: Adjust Class Access Modifiers

If you want to keep the class structure but allow plain object assignment, you can remove the private modifiers (switch to public, or protected if inheritance is needed). This makes TypeScript use structural typing instead of relying on class identity:

class User {
  public name: string;
  public address: Address[];
  constructor(name: string, address: Address[]) {
    this.name = name;
    this.address = address;
  }
  // Keep setters/getters if needed
  public set _name(name: string) {
    this.name = name;
  }
  public get _name() {
    return this.name;
  }
}

class Address {
  public city: string;
  constructor(city: string) {
    this.city = city;
  }
  public set _city(city: string) {
    this.city = city;
  }
  public get _city() {
    return this.city;
  }
}

// Now plain objects match the class structure and are allowed
let users: User[] = [{name: 'Naveen', address: [{'city': 'Bangalore'}]}];

内容的提问来源于stack exchange,提问作者Jonathan Monet

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 06:31:24