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

TypeScript中如何在new实例时填充对象?接口初始化报错求助

Let's break down your questions one by one—both are common TypeScript pitfalls around object initialization and generic class instantiation:

1. Why your translateObj initialization isn't working (and the fix)

First off, your current syntax has two clear issues:

  • You’re using new with translateObj, but translateObj is a variable typed as ITranslate—interfaces aren’t classes, so you don’t need the new keyword to create objects that match their structure.
  • The chained assignment let obj = new translateObj = { ... } is invalid here; it doesn’t make logical sense to assign an object literal to translateObj and then pass that result to obj in one line like this.

Correct way to initialize an ITranslate object

Since ITranslate is an interface (a TypeScript type constraint), you just need to create an object that matches its structure and assign it directly to your variable:

// First, confirm your interface is defined (I'll assume this exists):
interface ITranslate {
  keyTranslate: string;
  outName: string;
}

// Initialize the object correctly
let translateObj: ITranslate = {
  keyTranslate: 'subjectId',
  outName: 'name'
};

This works because TypeScript only checks that the object literal has all the required properties from the ITranslate interface—no constructor or new keyword is needed here.

2. Instantiating your generic MapperServiceArray<T> class

Your generic class has a constructor that takes a required key parameter and an optional translate parameter of type ITranslate. Here’s how to create instances properly:

Quick context (for clarity)

I’ll assume your IMapperServiceArray<T> interface looks something like this (since you didn’t share the full definition):

interface IMapperServiceArray<T> {
  key: string | number;
  translate?: ITranslate;
  // Add any methods your interface requires, e.g.:
  mapArray(data: T[]): any[];
}

Case 1: Create an instance without the translate parameter

You can omit the optional translate argument entirely. Just make sure to specify the generic type T (or let TypeScript infer it automatically):

// Example: Let's say T is a `User` type you've defined
interface User {
  id: number;
  fullName: string;
}

// Explicitly specify T when instantiating
const userMapper = new MapperServiceArray<User>('userId');

Case 2: Create an instance with the translate parameter

Use the properly initialized translateObj we created earlier, or pass an inline object that matches the ITranslate structure:

// Using the pre-defined translateObj
const userMapperWithTranslate = new MapperServiceArray<User>('userId', translateObj);

// Or passing an inline object literal (no need for a separate variable)
const userMapperWithInlineTranslate = new MapperServiceArray<User>('userId', {
  keyTranslate: 'subjectId',
  outName: 'name'
});

Bonus: Let TypeScript infer the generic type

If you later use methods on the MapperServiceArray instance that work with T-typed data, TypeScript can often infer T automatically, so you don’t have to specify it explicitly:

// TS will infer T as User when we call mapArray with a User[]
const autoInferMapper = new MapperServiceArray('userId');
autoInferMapper.mapArray([{ id: 1, fullName: 'Alice' }]);

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 08:30:23