這是我從后端得到的回應。我想為此撰寫一個介面模型,其中 userIds 可以是任意數字。如何為此撰寫模型?
{
"courseId": 1,
"userIds": [
"46071424",
"46076456",
],
"endDate": "2022-03-29"
}
uj5u.com熱心網友回復:
您應該查看 Typescript 的介面檔案。https://www.typescriptlang.org/docs/handbook/2/objects.html
您示例回應中的 UserId 看起來像字串。
因此對于
{
"courseId": 1,
"userIds": [
"46071424",
"46076456",
],
"endDate": "2022-03-29"
}
你的界面可能是
interface IModel {
courseId: number;
userIds: string[];
endDate: string;
}
uj5u.com熱心網友回復:
export interface Course {
courseId: number;
userIds: string[];
endDate: string;
}
uj5u.com熱心網友回復:
TS 物件型別
嘗試這個:
interface number{
courseId: number;
userIds: any;
endDate: string;
}
uj5u.com熱心網友回復:
https://www.typescriptlang.org/docs/handbook/interfaces.html
// model 1
interface backendModel{
courseId : number,
userIds? : string[],
endDate?: string
}
// ex :
const model : backendModel =
{
"courseId": 1,
"userIds": [
"46071424",
"46076456",
],
"endDate": "2022-03-29"
};
// model 2
interface backendModel2{
courseId : Number,
userIds? : Number[],
endDate?: Date
}
const model2 : backendModel2 =
{
"courseId": 1,
"userIds": [
Number("46071424"),
Number( "46076456"),
],
"endDate": new Date("2022-03-29")
};
// model 3 if all fields are required
interface backendModel3{
courseId : number,
userIds : string[],
endDate : string
}
uj5u.com熱心網友回復:
試試http://json2ts.com/。您可以將任何 JSON 轉換為 TS
uj5u.com熱心網友回復:
日期資料型別可用于獲取角度日期,因為您的資料為字串形式您也可以使用字串。
export interface AuthResponse{
courseId: number;
userIds : string[];
endDate : Date;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/375452.html
