TypeScript中如何在new实例时填充对象?接口初始化报错求助
Let's break down your questions one by one—both are common TypeScript pitfalls around object initialization and generic class instantiation:
translateObj initialization isn't working (and the fix) First off, your current syntax has two clear issues:
- You’re using
newwithtranslateObj, buttranslateObjis a variable typed asITranslate—interfaces aren’t classes, so you don’t need thenewkeyword 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 totranslateObjand then pass that result toobjin 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.
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

