0

This is the response I am getting from the backend. I want to write an interface Model for this where userIds can be any number. How to write a model for this?

{
    "courseId": 1,
    "userIds": [
      "46071424",
      "46076456",
    ],
    "endDate": "2022-03-29"
}

6 Answers 6

3

You should check out the interface docs for Typescript. https://www.typescriptlang.org/docs/handbook/2/objects.html

UserId in you example response look like strings.

So for

{
    "courseId": 1,
    "userIds": [
      "46071424",
      "46076456",
    ],
    "endDate": "2022-03-29"
}

your interface could be

interface IModel {
  courseId: number;
  userIds: string[];
  endDate: string;
}
Sign up to request clarification or add additional context in comments.

Comments

2
export interface Course {
  courseId: number; 
  userIds: string[];
  endDate: string;
}

Comments

1

TS Object Types

Try this:

interface number{
   courseId: number;
   userIds: any;
   endDate: string;
}

Comments

0

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
}

Comments

0

Just try http://json2ts.com/. You can convert any JSON to TS from it

Comments

0

Date datatype can be used to get the date in angular since your data in the form of string You can use string too.

export interface AuthResponse{
    
  courseId: number; 
  userIds : string[];
  endDate : Date;
 
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.