typescript 为什么我的类有私有属性,而我给予它一个有公共属性的对象,这在类型脚本中是有问题的?

14ifxucb  于 2023-06-07  发布在  TypeScript
关注(0)|答案(1)|浏览(148)

我试图写一个带有私有属性的typescript类,但这些属性都是在构造函数中设置的,构造函数只需要一个对象,但我总是得到一个错误,即“error TS 2345:“TranslateResponse”类型的参数不可分配给“{ status:“成功”|“failed”; errorCode?:数量|未定义;消息?:字符串|undefined; data?:any;}'. 2023-05-30 13:45:24 translator-angular|属性“status”在类型“TranslateResponse”中是私有的,但在类型“{ status:“成功”|“failed”; errorCode?:数量|未定义;消息?:字符串|undefined; data?:any;}'。我不明白,如果一个对象的属性是私有的,或者是公有的,它有什么意义?我读了一些关于这个的故事,但他们都是接口的问题,但我没有任何接口。

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;
    }

这个构造函数在这里得到一个对象:

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));
                }
            });
        }
    }

它是另一个对象的构造函数。错误指向FileTranslatedSuccess类的示例化。(this.fileTranslatedSuccess[key].push(new FileTranslatedSuccess(fileTranslatedSuccess));).有人能给我解释一下,为什么它是一个错误,如果我设置私有属性在构造函数从一个对象与公共属性?

plupiseo

plupiseo1#

object.fileTranslatedSuccess[key].forEach(fileTranslatedSuccess => {
    this.fileTranslatedSuccess[key].push(new FileTranslatedSuccess(fileTranslatedSuccess));
});

Typescript假定fileTranslatedSuccess的类型为FileTranslatedSuccess。Typescript在编译期间不读取fileTranslatedSuccess的实时值,并假定只有FileTranslatedSuccess类中的公共属性可用。即使fileTranslatedSuccess的值在运行时具有newFilePath、newFileName、targetLanguage的有效公共值,Typescript也不知道也不关心。
你混淆了TypeScript编译器。从某种意义上说,你是在声明这3个属性应该从公共视图中隐藏,但在下一个你是在请求公共视图,以便你可以从一个示例分配值到下一个。
要解决此问题,您可以
1.使属性公开
1.使getter函数返回每个属性的值。就像

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;
 }

 getFilePath(): string {
     return this.newFilePath;
 }

 getFileName(): string {
     return this.newFileName;
 }

 getTargetLanguage(): string {
     return this.targetLanguage;
 }

}
然后在调用中可以执行以下操作

object.fileTranslatedSuccess[key].forEach(fileTranslatedSuccess => {
    const filePath = fileTranslatedSuccess.getFilePath();
    const fileName = fileTranslatedSuccess.getFileName();
    const targetLanguage = fileTranslatedSuccess.getTargetLanguage();
    
    this.fileTranslatedSuccess[key].push(new FileTranslatedSuccess({newFilePath: filePath, newFileName: fileName, targetLanguage: targetLanguage}));
});

1.如果object.fileTranslatedSuccess来自API调用,并且您正在尝试将Object示例化为FileTranslatedSuccess类型,则使用接口可能符合您的最佳利益。在接口中将这3个属性设置为公共属性,然后在构造函数中正常赋值。这将有助于与其他任何人在你的项目工作的清晰度。下面是一个基本的例子(未测试)。

export interface IFileTranslatedSuccess {
     newFilePath: string;
     newFileName: string;
     targetLanguage: LanguageCode;
 }

 object.fileTranslatedSuccess[key].forEach((ifileTranslatedSuccess: IFileTranslatedSuccess) => {     
     this.fileTranslatedSuccess[key].push(new FileTranslatedSuccess(ifileTranslatedSuccess));
 });

然后在FileTranslatedSuccess类中,

export class FileTranslatedSuccess {
    
    private newFilePath!: string;
    private newFileName!: string;
    private targetLanguage!: LanguageCode;

    constructor(object: IFileTranslatedSuccess) {
       this.newFilePath = object.newFilePath;
       this.newFileName = object.newFileName;
       this.targetLanguage = object.targetLanguage;
       ...
    }
}

相关问题