I am currently facing an issue while attempting to create a typescript class with private properties that are initialized in the constructor using an object. Unfortunately, I keep encountering an error message stating: "error TS2345: Argument of type 'TranslateResponse' is not assignable to parameter of type '{ status: "success" | "failed"; errorCode?: number | undefined; message?: string | undefined; data?: any; }'. 2023-05-30 13:45:24 translator-angular | Property 'status' is private in type 'TranslateResponse' but not in type '{ status: "success" | "failed"; errorCode?: number | undefined; message?: string | undefined; data?: any; }'. I'm confused about the significance of declaring a property as private or public within an object. Although I've come across similar issues concerning interfaces, I don't actually have any interfaces defined in my code.
export class FileTranslatedSuccess {
private newFilePath!: string;
private newFileName!: string;
private targetLanguage!: LanguageCode;
constructor(object: {newFilePath: string, newFileName: string, targetLanguage: LanguageCode}) {
this.newFilePath = object.newFilePath;
this.newFileName = object.newFileName;
this.targetLanguage = object.targetLanguage;
}
The constructor above takes an object of the following shape:
constructor(object: {
fileTranslatedSuccess: {[key: string]: FileTranslatedSuccess[]},
fileTranslatedFailed: {originalFile: string, targetLanguage: LanguageCode, message: string}[] | ""
}) {
let keys = Object.keys(object.fileTranslatedSuccess);
this.fileTranslatedSuccess = {};
keys.forEach(key => {
this.fileTranslatedSuccess[key] = [];
object.fileTranslatedSuccess[key].forEach(fileTranslatedSuccess => {
this.fileTranslatedSuccess[key].push(new FileTranslatedSuccess(fileTranslatedSuccess));
});
});
if (object.fileTranslatedFailed === "") {
this.fileTranslatedFailed = "";
} else {
this.fileTranslatedFailed = [];
object.fileTranslatedFailed.forEach(fileTranslatedFailed => {
if (Array.isArray(this.fileTranslatedFailed)) {
this.fileTranslatedFailed.push(new FileTranslatedFailed(fileTranslatedFailed));
}
});
}
}
This constructor belongs to another object in my codebase. The error occurs when trying to instantiate the FileTranslatedSuccess class. (this.fileTranslatedSuccess[key].push(new FileTranslatedSuccess(fileTranslatedSuccess));). Can someone clarify why setting private properties in the constructor using an object with public properties results in an error?