[英]Typescript - Object is possibly 'undefined' in interface
在 Angular 項目中,我使用的是接口。
export interface School {
name: string = '';
website: string;
registrationNumber: string;
dateEstablished: Date;
address: string;
country: [];
companyLogo: any = '';
clear() {
this.name = '';
this.website = '';
this.registrationNumber = '';
this.dateEstablished = null;
this.address = '';
this.country = [];
this.companyLogo = '';
}
}
我有兩個錯誤:
Object 可能是“未定義的”——以及所有的“這個”。 突出顯示
聲明或聲明 expected.ts(1128) - 最后一個右大括號突出顯示。
預期的呼號:'clear' 有一個 typedef (typedef)tslint(typedef)
我如何獲得這些決心?
謝謝
您需要像這樣實現該接口:
文件:school.ts
interface ISchool {
name: string = '';
website: string;
registrationNumber: string;
dateEstablished: Date;
address: string;
country: [];
companyLogo: any = '';
clear(): () => void;
}
export class School implements ISchool {
// Declare all attributes here;
public name: string = '';
...
clear() {
this.name = '';
this.website = '';
this.registrationNumber = '';
this.dateEstablished = null;
this.address = '';
this.country = [];
this.companyLogo = '';
}
}
文件 app.ts
import {School} from 'school.ts';
const someSchool = new School();
someSchool.clear();
這是一個基本的 object 定位技術,所以我建議查看類和接口的使用。
但是,如果您只有一種學校,則不需要該界面。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.