javascript - 如何使用 typescript 处理 json 对象

标签 javascript node.js typescript

这是我的正常虚拟数据“.js”
有一个数组并将其导出。

const students = [
  {
    id: 1,
    name: 'Sean Grey',
    age: 24
  },
  {
    id: 2,
    name: 'Sllllean Grey',
    age: 224
  },
  {
    id: 3,
    name: 'Seansdajfklajs Grey',
    age: 2114
  }
];

export default students;

我想导出这个虚拟数据检查 typescript 。
所以我想使用“类”或“接口(interface)”。
但我不知道这个例子的指南。 你能为我推荐一些建议吗?

最佳答案

听起来您正在寻找一种声明对象结构和字段的方法,这就是 interface是为了。

示例:

interface Student {
    id: number;
    name: string;
    age: number;
}

用法:

const students: Student[] = [{
    id: 1,
    name: 'Sean Grey',
    age: 24
}];

关于javascript - 如何使用 typescript 处理 json 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58254683/

相关文章:

javascript - MongoDB 在集合中查找最新日期

node.js - 你如何更新 Node.js 中的一对多关系?

typescript - Testcafe Docker - 错误 : TypeScript compilation failed. 找不到名称 'require'

html - 如何使用 PrimeNG 在标题中显示保存的过滤器?

php - 清空 libs.css 和 libs.js 文件

angular - Primeng组件日历有手动关闭弹出日历的方法吗?

javascript - 动态创建 id 时无法使用 jquery 显示/隐藏 div

javascript - fullpage.js - 在不在 fullpage.js 包装器中的 div 内滚动在 IOS 上是不可能的

javascript - Quickblox 群聊加入不工作(Javascript SDK)

Javascript 对象最大内存大小