I want to define the type in the Json file (associative array) returned from the API in TypeScript.

Asked 1 years ago, Updated 1 years ago, 47 views

If you go to the URL below, the data from the GitHub repository will be returned as a Json file. Could you tell me how to define the type for items with multiple objects in the array?
https://api.github.com/repositories/354726603/contents/articles

I thought I could refer to this article, but the data returned is [{}, {}, {}...] and the number of data returned is expected to increase in the future.
When that happens, I don't know how to define the type.
Could you please help me?Please.

I want to type fetch results

type Articles={
  id:string;
  name —string;
  age:number;
  created_at:number;
  updated_at:number;
};

typescript

2022-09-30 17:33

1 Answers

I wrote it in my own way. I wrote the definition of the type article and the image that it was arranged.
Now I think I can define the type itself.

typeArticle={
  name —string;
  sha —string;
  size:number;
  url:string;
  html_url:string;
  git_url:string;
  download_url:string;
  type:string;
  _links:{
    self:string;
    git:string;
    html:string;
  }
};

constzennArticles: Article[] = wait fetch("https://api.github.com/repos/wimpykid719/zenn-content/contents/articles/")
    .then(res=>{
        return res.json();
    })
    .catch(err=>{
        console.log(err);
    });


2022-09-30 17:33

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.