typescript中interface,type和Record的使用

news2024/10/3 2:23:58

vite+vue3+ts中interface,type和Record的使用

  • vite+vue3+ts中interface,type和Record的使用
    • interface:接口
    • type:类型别名
      • 基本类型
      • 组合
      • 元组
      • 类型捕捉
      • 遍历属性
    • 扩展
      • interface扩展interface(合并)
      • interface扩展interface(继承)
      • type扩展type(合并)
      • interface扩展type
      • type扩展interface
    • Record
      • 简单使用
      • 复杂对象
    • 参考链接

interface:接口

中文官方文档:https://www.tslang.cn/docs/handbook/interfaces.html

type:类型别名

https://www.tslang.cn/docs/handbook/advanced-types.html
https://www.typescriptlang.org/docs/handbook/2/keyof-types.html

基本类型

type Person = string;

组合

interface People {
  name: string,
  weight: number
}
type Animal = {
  name: string,
}
type Cat = People | Animal

type Fruit = 'apple' | 'pear' | 'orange';
type Vegetable = 'broccoli' | 'carrot' | 'lettuce';

// 'apple' | 'pear' | 'orange' | 'broccoli' | 'carrot' | 'lettuce';
type HealthyFoods = Fruit | Vegetable;

const bf: HealthyFoods = 'broccoli';
console.log(bf);

const useDemo = function (food: HealthyFoods) {
  console.log(`Eating ${food}`);
}
useDemo('carrot');

元组

元组将不同类型的值按照预定义的顺序组合在一起,每个位置都有固定的类型,且位置的顺序在定义时是确定的

type ResponseCode = [string, number];
interface Dog {
  name: string;
}
interface Cat {
  age: number;
}
type Animal = [Dog, Cat];

// 创建一个 Animal 元组实例
const animal1: Animal = [
  { name: "Fido" },
  { age: 3 }
];
console.log(animal1[0].name); // 输出: "Fido"
console.log(animal1[1].age); // 输出: 3

类型捕捉

const orange = { color: "Orange", vitamin: 1}
// { color: string, vitamin: number }
type Fruit = typeof orange
let apple: Fruit;
apple = { color: "Red", vitamin: 2 }

type 会自动捕捉到 color 是 string 类型,vitamin 是 number 类型

let div = document.createElement("div");
type B = typeof div; // HTMLDivElement

遍历属性

type Keys = "firstName" | "lastName";

type Name = {
  [key in Keys]: string;
};

const myName: Name = {
  firstName: "kelen",
  lastName: "huang",
};

扩展

interface 总是可扩展的

interface扩展interface(合并)

interface Animal {
  name: string
}
interface Animal {
  honey: boolean
}
// 定义bear的实例
const bear: Animal = {
  name: 'wine',
  honey: true
}

interface扩展interface(继承)

interface Animal {
  name: string
}
interface Bear extends Animal {
  honey: boolean
}

const bear: Bear = {
  name: 'wine',
  honey: true
}
// 如果不写honey属性就会报错 

type扩展type(合并)

type创建后不能更改,不能通过同一个名称扩展,只能通过&去扩展

type Animal {
  name: string
}
type Bear & Animal {
  honey: boolean
}
// 定义bear的实例
const bear:Bear = {
  name: 'wine',
  honey: true
}

interface扩展type

type Animal = {
  name: string,
}

interface Bear extends Animal {
  honey: boolean;
}

type扩展interface

interface Animal {
  name: string,
}

type Bear = Animal & {
  honey: boolean
}

Record

https://www.typescriptlang.org/docs/handbook/utility-types.html#recordkeys-type

  • Record<key type, value type> 第一个key值类型,第二个为obj[key]数据的类型
  • Record<string, unknown> 任意对象

简单使用

let termMap1 = {} as Record<string, string>;
// 定义对象 Record<string, string> 第一个string为key值,第二个为obj[key]数据的类型
termMap1 = {
  age:10, // 不能将类型“number”分配给类型“string”。
  num:'11'
}
console.log('termMap1',termMap1 );

复杂对象

// Record 来限制 对象的key和value
type P = {
  [key: string]: number;
};
let p: P = { ppp: 10, ooo: 10 };
// k不是泛型  K泛型

// 01 Record 来限制 对象的key和value
type PP = Record<string, number>;
let pp: PP = {
  ppp: 10,
  ooo: 10,
};

type PP2 = Record<string, string>;
let pp2: PP2 = {
  ppp: "10", //  ppp: '10' 不能将类型“number”分配给类型“string”
  ooo: "10",
};

// 02 Record 来限制 复杂对象的key和value
interface term {
  info: number;
  checked: boolean;
}
type TermMap = Record<string, term>;
// item项目  key:string  value: { info: 10, checked: true }
let termMap: TermMap = {
  xxx: {
    info: 10,
    checked: true,
  },
};

// 03 Record 来限制 复杂对象的key和value
interface info_config {
  name: string;
}
interface term2 {
  info: info_config;
  checked: boolean;
}
let termMap2 = {} as Record<string, term2>;
// 定义对象 Record<string, string> 第一个string为key值,第二个为obj[key]数据的类型
termMap2 = {
  xxx: {
    info: {
      name: "111",
    },
    checked: true,
  },
};

// 04 Record 来限制 复杂对象的key和value
interface info_config3 {
  name: string[];
}
interface term3 {
  info: info_config3;
  checked: boolean;
}
let termMap3 = {} as Record<string, term3>;
// 定义对象 Record<string, string> 第一个string为key值,第二个为obj[key]数据的类型
termMap3 = {
  xxx: {
    info: {
      name: ["1", "2"],
    },
    checked: true,
  },
};

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/686298.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

你以为的网络工程师VS实际工作中的网络工程师

我的网工朋友&#xff0c;大家好。 前两天不是端午节嘛&#xff0c;去亲戚家吃饭。饭后闲聊说起&#xff0c;他们家的电脑开不了机了&#xff0c;问我能不能修下。 我说我不太会&#xff0c;让他们打电话报修&#xff0c;会有人上门看的。 结果亲戚蹦出一句话&#xff1a;你…

Qt Widget提升为QChartView绘制曲线

1、在工程文件"*.pro"中添加"charts"组件 在工程文件"*.pro"中添加"charts"组件&#xff0c;同时在工程文件添加qcustomplot类。 QT core gui printsupport charts下载qcustomplot类。 网址&#xff1a;https://www.qcustom…

技术小知识:分布式中的BASE和CAP原则 ③

一、CAP 理论和 BASE 理论 理论是指导业界实现的纲领&#xff0c;也是提炼了多年研究的精华&#xff0c;在分布式一致性领域&#xff0c;最主要的指导理论是 CAP 和 BASE 两个。 二、CAP理论 CAP原则又称CAP定理&#xff0c;指的是在一个分布式系统中&#xff0c; Consistency&…

面试官:SpringBoot如何快速实现分库分表?

一、什么是 ShardingSphere&#xff1f; shardingsphere 是一款开源的分布式关系型数据库中间件&#xff0c;为 Apache 的顶级项目。其前身是 sharding-jdbc 和 sharding-proxy 的两个独立项目&#xff0c;后来在 2018 年合并成了一个项目&#xff0c;并正式更名为 ShardingSp…

低代码搭建100分的酷炫大屏看板,3分钟打动老板!

不久前的一个热门话题是“00后整顿职场”&#xff0c;其实完全是胡说八道&#xff0c;因为大半的00后连工作都找不到&#xff01; 在行业危机&#xff0c;裁员话题不时火爆的今天&#xff0c;别说00后了&#xff0c;90后的打工人们纷纷都有了人还没到中年的就有的危机感。别说升…

【基于Django框架的在线教育平台开发-01】账号登录及退出登录功能开发

文章目录 1 模型层开发2 视图层开发3 form表单验证4 配置urls.py5 模板层开发6 效果展示 1 模型层开发 用户数据表如下所示&#xff1a; FieldTypeExtraidintPrime Key & Auto Incrementpasswordvarchar(128)last_logindatetime(6)Allow Nullis_superusertinyint(1)usern…

适用于 SAP 解决方案的 OpenText Extended ECM(企业内容管理)

适用于SAP 解决方案的 Extended ECM 概述 创建一种更好的将您的企业内容和企业应用程序连接起来工作方式&#xff0c;并从全面的数字内容管理平台中受益&#xff0c;该平台以产品化的方式无缝集成到任何 SAP 业务应用程序中&#xff0c;无论是在本地还是在云中。 SAP 解决方案…

Flutter 组件(二)文本 与 输入框组件

Flutter开发笔记 Flutter 组件&#xff08;二&#xff09;文本 与 输入框组件 - 文章信息 - Author: Jack Lee (jcLee95) Visit me at: https://jclee95.blog.csdn.netEmail: 291148484163.com. Shenzhen ChineAddress of this article:https://blog.csdn.net/qq_28550263/art…

第一章 计算机系统的概述①

一、操作系统概述 1、操作系统的概念&#xff08;什么是操作系统&#xff09; 概念&#xff1a;操作系统 (Operating System&#xff0c; 0s) 是指控制和管理整个计算机系统的硬件和软件资源&#xff0c;并合理地组织调度计算机的工作和资源的分配:以提供给用户和其他软件方便…

✅【值得收藏】超全期刊缩写查询网址

【SciencePub学术干货】英文论文写作中会插入参考文献&#xff0c;而参考文献中的期刊名称时常需要使用缩写。期刊缩写一般包括两种格式&#xff1a;JCR缩写和ISO缩写。比如 Journal of controlled release 杂志&#xff1a; 期刊名&#xff1a;JOURNAL OF CONTROLLED RELEASE…

力扣算法刷题Day47|周日总结:动态规划之背包问题

背包问题 〉题型分类 解题套路 〉动规五部曲 确定dp数组&#xff08;dp table&#xff09;以及下标的含义确定递推公式dp数组如何初始化确定遍历顺序举例推导dp数组 解题技巧 〉递推公式 问背包装满后的最大价值&#xff1a;dp[j] max(dp[j], dp[j - weight[i]] value[i]) …

跨库分页查询

背景 随着数据量的增大&#xff0c;数据库需要进行水平切分&#xff0c;例如通过业务主键id取模&#xff0c;使得数据均匀分布到不同的库中&#xff0c;随之而来的问题就出现跨库如何进行分页查询。 举例 select * from t_user order by time offset 200 limit 100 当在单库…

NXP i.MX 6ULL工业核心板规格书( ARM Cortex-A7,主频792MHz)

1 核心板简介 创龙科技SOM-TLIMX6U是一款基于NXP i.MX 6ULL的ARM Cortex-A7高性能低功耗处理器设计的低成本工业级核心板&#xff0c;主频792MHz&#xff0c;通过邮票孔连接方式引出Ethernet、UART、CAN、LCD、USB等接口。核心板经过专业的PCB Layout和高低温测试验证&#xf…

Jenkins在Ubuntu的安装问题

使用apt安装没有成功&#xff0c;各种报错。最后使用了离线安装方式。 1、安装jdk。和之前的安装jdk无异&#xff0c;增加一步 添加一个软链接 sudo ln -s /path/to/java/home/bin/java /usr/bin/java 2、下载deb包&#xff0c;然后安装 2.1、前置步骤&#xff0c;安装可能…

面向适航符合性的智能航电系统认证研究进展

摘要 民用飞机航电系统引入人工智能/机器学习技术会带来可信性、不确定性和可解释性等问题&#xff0c;有必要通过有效的符合性方法向公众与利益攸关方证实智能航电系统的适航安全性。首先&#xff0c;分析了智能航电系统的等级分类和应用现状&#xff0c;阐述了现有指南和标准…

three.js通过CubeTexture加载环境贴图,和RGBELoader加载器加载hdr环境贴图

一、使用CubeTexture进行环境贴图 1.CubeTexture使用介绍 Three.js中可以通过使用CubeTexture进行环境贴图&#xff0c;CubeTexture需要将6张图片&#xff08;正面、反面、上下左右&#xff09;包装成一个立方体纹理。下面是一个简单的例子&#xff1a; 首先需要加载六张贴图…

关于随机梯度下降算法及其改进方向

回归与分类等监督学习是机器学习中最常见的一类学习问题, 它提供了包含输入数据和目标数据的训练数据集。为了探讨输入与目标之间的关系&#xff0c;需要先建立含参数的表示模型&#xff0c;再通过最小化所有样本的平均损失函数来获得最优的参数, 此处的优化模型通常为经验风险…

【SpringMVC】统一异常处理 前后台协议联调 拦截器

1&#xff0c;统一异常处理 1. 问题描述 在讲解这一部分知识点之前&#xff0c;我们先来演示个效果&#xff0c;修改BookController类的getById方法 GetMapping("/{id}") public Result getById(PathVariable Integer id) {//手动添加一个错误信息if(id1){int i …

那些曾经考过的turtle绘图题(16~20)

【编程实现绘图 -16】 使用turtle绘制如右图1中所示的图形。 上边是一个红色轮廓、黄色填充的边长为300的等边三角形,下边是一个绿色填充、半径为150的半圆。 要求: 1)画布背景为白色,等边三角形为红色轮廓、黄色填充; 2)半圆为绿色填充、且与等边三角形在底边的中点处相…

OpenCV——总结《车牌识别》之《常用的函数介绍》

1. cv2.getStructuringElement(cv2.MORPH_RECT, (10, 10))element cv2.getStructuringElement(shape, ksize[, anchor])用于创建形态学操作的结构元素&#xff08;structuring element&#xff09;。 参数解释&#xff1a; shape&#xff1a;结构元素的形状&#xff0c;可以…