首页 > 其他分享 >兼收并蓄 TypeScript - 基础: 数据类型

兼收并蓄 TypeScript - 基础: 数据类型

时间:2024-09-20 12:03:02浏览次数:1  
标签:TypeScript console log 数据类型 兼收并蓄 let typeof string

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

兼收并蓄 TypeScript - 基础: 数据类型

示例如下:

basic\dataType.ts

{
    // 基本数据类型 boolean, number, string, symbol
    let a: boolean = true;
    let b: number = 10;
    let c: string = "abc";
    let d: symbol = Symbol();

    // 对象数据类型,即通过 new 出来的对象
    let e: Date = new Date();
    // 定义为 object 类型,则可以赋值为任意对象
    let f: object = new Date();

    // 定义为 any 类型,则可以赋值为任意基本类型或任意对象
    let g: any = "xyz";
    g = new Date();
}

{
    // 定义变量的时候如果没有指定数据类型,但是赋值了,则 TypeScript 会自动推导出变量的数据类型
    let a = 10;
    console.log(typeof a); // number

    // 定义变量的时候如果没有指定数据类型,也没有赋值,则 TypeScript 会认为变量的数据类型是 any
    let b;
    console.log(typeof b); // undefined
    b = 10;
    console.log(typeof b); // number
    b = "abc";
    console.log(typeof b); // string
}

{
    let a = 10;
    let b = new Date()

    // 通过 typeof 判断数据类型,可以判断基本类型
    console.log(typeof a == 'number'); // true

    // 通过 typeof 判断数据类型,如果是对象类型,则获取到的一律是 object,无法获取到具体的对象类型
    console.log(typeof b == 'object'); // true

    // 通过 typeof 也可以判断是否是 function
    console.log(typeof b.getDate == 'function'); // true

    // 如果是对象类型,则需要通过 instanceof 判断具体的对象类型
    console.log(b instanceof Date); // true
}

{
    // 联合类型,可以定义变量为多个数据类型中的一个
    let a: number | string;
    a = 10;
    console.log(typeof a); // number
    a = "abc";
    console.log(typeof a); // string
}

{
    // 注:请注意区分类似如下的情况
    // 比如 new Number() 和 Number(), 前者是是一个类的构造函数用于实例化对象,后者是全局函数返回的是一个基本类型
    let a = new Number("10");
    let b = Number("10");
    console.log(typeof a, typeof b); // object number
}

{
    // 也可以这么声明数据类型
    let a: {key: string, value: string} = {key: "key", value: "value"};
    console.log(a.key, a.value); // key value
}

{
    // if 会将 0, "", null, undefined, NaN 隐式地转换为布尔值 false
    if (!0 && !"" && !null && !undefined && !NaN) {
        console.log("if 条件的隐式转换");
    }
}

{
    // 通过 type 指定类型别名
    type Name = string;
    type NameMethod = () => string;
    type NameOrMethod = Name | NameMethod;
    function getName(p: NameOrMethod): Name {
        if (typeof p === 'string') {
            return p;
        } else {
            return p();
        }
    }

    console.log(getName("webabcd")); // webabcd
    console.log(getName(() => "webabcd")); // webabcd
}

{
    class Animal {
        constructor(public name: string) { 
        }
    }
    class Dog extends Animal {
        run() {
            console.log("run");
        }
    }

    function f1(animal: Animal)
    {
        // 通过 instanceof 判断一个基类对象是否是某个子类对象
        if (animal instanceof Dog) 
        {
            // 通过 as 将基类对象转换为子类对象
            let dog = animal as Dog;
            dog.run();
        }
    }
    f1(new Dog("dog"));
}

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

标签:TypeScript,console,log,数据类型,兼收并蓄,let,typeof,string
From: https://www.cnblogs.com/webabcd/p/18422223/typescript_basic_dataType

相关文章

  • 兼收并蓄 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 数据类型转换详解:隐式转换(自动转换)与强制转换(手动转换)
    目录前言取值范围从小到大的关系:隐式转换(自动转换)......
  • 鸿蒙原生应用元服务开发-仓颉基础数据类型字符类型
    字符类型使用Rune表示,可以表示Unicode字符集中的所有字符。字符类型字面量字符类型字面量有三种形式:单个字符、转义字符和通用字符。一个Rune字面量由字符r开头,后跟一个由一对单引号或双引号包含的字符。单个字符的字符字面量举例:leta:Rune=r'a'letb:Rune=r"b"转......
  • 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......
  • JavaScript语法入门七 数据类型
     BigInt类型在JavaScript中,“number”类型无法代表大于 253(或小于 -253)的整数。此时可以使用BigInt类型。使用方法:在数字的尾部附加一个n。constbigInttest=12345678901234567890123456789012345678901121345526789n; String类型js中只有String类型没有char类型。定义时......
  • D11【python接口自动化学习】-python基础之内置数据类型
    day11列表的常见操作学习日期:20240918学习目标:内置数据类型--20列表的常见操作学习笔记:添加元素#创建列表list_demo=['a','b','c','d']print(type(list_demo))#<class'list'>print(list_demo)#['a','b','c�......
  • [Clickhouse] Clickhouse 函数 : 数据类型转换
    0引言如无特殊说明,ck版本为21.3.4.251数据类型的支持情况查看当前受支持的数据类型select*fromsystem.data_type_families--selectname,case_insensitive,alias_tofromsystem.data_type_families;outputname|case_insensitive......
  • java_day2_常量,变量,数据类型,运算符
    一、常量常量:在Java程序运行过程中其值不能发生改变的量分类:1、字面值常量:整数常量表示所有的整数,包括负数10-8小数常量表示所有的小数1.23-3.14布尔常量truefalse空常量null......