首页 > 其他分享 >兼收并蓄 TypeScript - 类: interface

兼收并蓄 TypeScript - 类: interface

时间:2024-09-20 12:13:02浏览次数:1  
标签:TypeScript name Person 兼收并蓄 webabcd interface 属性 string

源码 https://github.com/webabcd/TypeScriptDemo
作者 webabcd

兼收并蓄 TypeScript - 类: interface

示例如下:

class\interface.ts

{
    // 接口用于定义对象的形状,这个是 TypeScript 的功能(JavaScript 中没有)
    interface Person {
        readonly id: number; // 只读属性
        name: string;
        age: number;
        hello(name: string): string; // 方法
    };
    
    // 变量的形状必须和接口的形状保持一致(也就是说属性名和类型必须与接口一样,且每个属性都必须要定义,且不能定义接口未定义的属性)
    let webabcd: Person = {
        id: 1234,
        name: 'webabcd',
        age: 44,
        hello(name: string): string {
            return 'hello: '+ name;
        }
    };

    webabcd.name = 'wanglei';
    console.log(webabcd, webabcd.hello('xxx')); // {id: 1234, name: 'wanglei', age: 44, hello: ƒ} hello: xxx
}

{
    interface Person {
        name: string;
        age?: number; // 通过 ? 定义可选属性
    };
    
    // 变量的形状必须和接口的形状保持一致,且可选属性可以不定义
    let webabcd1: Person = {
        name: 'webabcd',
    };
    let webabcd2: Person = {
        name: 'webabcd',
        age: 44
    };
}

{
    interface Person {
        name: string;
        [propName: string]: string | number; // 允许定义任意属性,并指定允许的属性的类型(注:接口中已定义的属性的类型也必须要符合此处指定的类型)
    };
    
    // 变量的形状必须和接口的形状保持一致,且可以新增其他任意属性(注:新增的属性的类型必须是接口中定义的类型)
    let webabcd: Person = {
        name: 'webabcd',
        age: 44,
        gender: 'male',
    };
}

{
    // 通过接口定义数组
    interface NumberArray {
        [index: number]: number;
    }
    let a: NumberArray = [1, 2, 3];    
    console.log(a); // [1, 2, 3]
}

源码 https://github.com/webabcd/TypeScriptDemo
作者 webabcd

标签:TypeScript,name,Person,兼收并蓄,webabcd,interface,属性,string
From: https://www.cnblogs.com/webabcd/p/18422253/typescript_class_interface

相关文章

  • 兼收并蓄 TypeScript - 基础: 基础
    源码https://github.com/webabcd/TypeScriptDemo作者webabcd兼收并蓄TypeScript-基础:基础示例如下:basic\basic.ts{//基础//try/catch/finally的用法functionf1(str:string):number|null{try{letnum=Number(str......
  • 兼收并蓄 TypeScript - 基础: var, let, const
    源码https://github.com/webabcd/TypeScriptDemo作者webabcd兼收并蓄TypeScript-基础:var,let,const示例如下:basic\var_let_const.ts//var声明的变量是全局作用域,块外也可用{vara=10;}console.log(a);//let声明的变量是块作用域,仅块内可用{......
  • 兼收并蓄 TypeScript - 基础: 数据类型
    源码https://github.com/webabcd/TypeScriptDemo作者webabcd兼收并蓄TypeScript-基础:数据类型示例如下:basic\dataType.ts{//基本数据类型boolean,number,string,symbolleta:boolean=true;letb:number=10;letc:string="abc";......
  • 兼收并蓄 TypeScript - 基础: null, undefined
    源码https://github.com/webabcd/TypeScriptDemo作者webabcd兼收并蓄TypeScript-基础:null,undefined示例如下:basic\null_undefined.ts{console.log(undefined==null,undefined===null);//truefalseconsole.log(typeofnull,typeofundefined);......
  • 兼收并蓄 TypeScript - 基础: boolean
    源码https://github.com/webabcd/TypeScriptDemo作者webabcd兼收并蓄TypeScript-基础:boolean示例如下:basic\boolean.ts{leta=true;console.log(a);//true//将指定类型的数据转换为boolean类型console.log(Boolean(100),Boolean(......
  • Java 接口interface
    目录1.定义接口2.实现接口3.多重继承4.抽象方法5.默认方法6.静态方法7.私有方法8.常量在Java中,接口(interface)是一种引用类型,它类似于类(class),但是与类不同的是,接口不能包含任何具体的方法实现(除了默认方法和静态方法之外)。接口定义了一组规则或行为,这些规则由实现该接......
  • react react18+vite+typeScript+eslint+prettier+husky+lint-staged+commitlint 快速
    技术栈react18react-router6antd5zustand4vite45axiosfakerjs模拟数据dayjslodashtypescriptechartscommitlint、prettier、eslinthusky、lint-staged自定义commitlint、cz-cli自定义eslint、prettier代码规范技术栈代码格式规范和语法检测vscode:统一前端编辑器。editor......
  • TypeScript入门 (二)控制语句
    引言大家好,我是GISerLiu......
  • Taobao API interface: keyword search product list data interface
    TaobaoAPIinterface:keywordsearchproductlistdatainterface——Ontheroadofgrowth,weareallfellowtravelers.IhopethisarticleabouttheTaobaoproductlistinformationinterfaceforproductselectioncanhelpyou.Ilookforwardtosharing......
  • EBS:OM Sales Order销售订单【Open Interface、Open API】
    21. OM Sales Order销售订单【Open Interface、Open API】21.1. 快速参考。参考点内容功能导航N: OM/Orders, Returns/Sales Order并发请求N: OM/View/Request/Order Import接口表oe_headers_iface_all/oe_lines_iface_all/oe_actions_iface_all/….APIoe_order_pub.pr......