TypeScript教程--js基本数据类型

128 阅读3分钟

TypeScript教程

js基本数据类型

说明:ts中数据类型包括js已有的数据类型、ts新增的数据类型,本文讲解js已有的数据类型。

①基本数据类型

  • string

  • number

  • boolean

  • null

  • undefined

  • symbol

  • bigint

②引用数据类型

  • object

  • array

  • function

    ....

基本数据类型

一、string

1.声明类型

①基本类型
let str_string: string
str_string = "a"
console.log("str_string type:", typeof str_string) //string
console.log(str_string.toUpperCase()) //A
//报错
//str_string = new String("a"); //TS2322: Type String is not assignable to type string
let a1 = "a"
let a2 = "a"
console.log(a1 == a2) //true
console.log(a1 === a2) //true
②包装类型
let str_String: String
str_String = "a"
console.log("str_String type:", typeof str_String) //string
console.log(str_String.toUpperCase()) //A
str_String = new String("a");
console.log("str_String type:", typeof str_String) //object
console.log(str_String.toUpperCase()) //A

备注:js在必要时会进行自动装箱,将基本类型转化成包装类型,以便调用方法或访问属性。

let a1 = new String("a")
let a2 = new String("a")
console.log(a1 == a2)//false
console.log(a1 === a2)//false

备注:String是string的包装类,string是基本数据类型, String是引用数据类型,通常情况下使用基本类型。

2.声明类型并赋初始值

①单双引号
let str_string2: string = "hello"
console.log("str_string2 value:", str_string2) //hello
str_string2 = "你好"
console.log("str_string2 value:", str_string2) //你好
let str_string3: string = 'hello'
console.log('str_string3 value:', str_string3) //hello
str_string3 = '你好'
console.log('str_string3 value:', str_string3) //你好
②模板字符串
let str_string4: string = `*${str_string3}*`
console.log("str_string4 value:", str_string4) //*你好*
str_string4 = '你好'
console.log("str_string4 value:", str_string4) //你好

二、number

let number1: number = 10
console.log("number1 type:", typeof number1) //number
console.log("number1 value:", number1) //10
let number2: number = 10.5
console.log("number2 type:", typeof number2) //number
console.log("number2 value:", number2) //10.5

三、boolean

let boolean1: boolean = true
console.log("boolean1 type:", typeof boolean1) //boolean
console.log("boolean1 value:", boolean1) //true
let boolean2: boolean = false
console.log("boolean2 type:", typeof boolean2) //boolean
console.log("boolean2 value:", boolean2) //false

四、null

let null1: null = null;
console.log("null1 type:", typeof null1) //object
//报错
//let null2:null=undefined;//TS2322: Type undefined is not assignable to type null
//let string_null:string=null //TS2322: Type null is not assignable to type string
//let number_null:number=null //TS2322: Type null is not assignable to type number
//let boolean_null:boolean=null //TS2322: Type null is not assignable to type boolean

备注:以上写法能否通过类型检查取决于tsconfig.json中strictNullChecks配置,默认为true,无法通过。

"strictNullChecks": true,  /* When type checking, take into account 'null' and 'undefined'.

五、undefined

let undefined1: undefined = undefined;
console.log("undefined1 type:", typeof undefined1) //undefined
//报错
//let undefined2:undefined=null;//TS2322: Type null is not assignable to type undefined
//let string_undefined:string=undefined //TS2322: Type undefined is not assignable to type string
//let number_undefined:number=undefined //TS2322: Type undefined is not assignable to type number
//let boolean_undefined:boolean=undefined //TS2322: Type undefined is not assignable to type boolean

备注:以上写法能否通过类型检查取决于tsconfig.json中strictNullChecks配置,默认为true,无法通过。

"strictNullChecks": true,  /* When type checking, take into account 'null' and 'undefined'.
console.log(null1 == undefined1) //true
console.log(null1 === undefined1) //false

六、symbol

说明:表示独一无二的值,使用Symbol()函数创建值,描述参数为可选的。

let symbol1: symbol
symbol1 = Symbol();
let symbol2: symbol
symbol2 = Symbol(1);
let symbol3: symbol
symbol3 = Symbol(1);

console.log("symbol1 type:", typeof symbol1) //symbol
console.log(symbol2 == symbol3) //false
console.log(symbol2 === symbol3) //false
const myProperty = Symbol('myProperty');
const obj = {
    [myProperty]: 'value',
    name: "张三"
};
console.log(Object.keys(obj)); //['name']

备注:作为对象的属性,可避免命名冲突,但并不会枚举出。

console.log(obj); //{name: '张三', Symbol(myProperty): 'value'}
console.log(JSON.stringify(obj)); //{"name":"张三"}
console.log(obj[myProperty]); //value
//报错
//console.log(obj.myProperty); // Property 'myProperty' does not exist on type '{ [myProperty]: string; name: string; }'.

备注:通过object[key]方式获取对应属性值,不能通过object.key方式获取对应值。

七、bigint(es2020即es11)

说明:用于表示任意精度的整数,number类型有一定范围限制(约±(2^53-1))。

let bigint1: bigint = 100n;
let bigint2: bigint = BigInt(200);
let addition: bigint = bigint1 + bigint2;
console.log(addition); //300n
let subtraction: bigint = bigint1 - bigint2; 
console.log(subtraction); //-100n
let multiplication: bigint = bigint1 * bigint2;
console.log(multiplication); //20000n
let division: bigint = bigint2 / bigint1;
console.log(division); //2n
let remainder: bigint = bigint2 % bigint1;
console.log(remainder); //0n

文章首发公众号自学Java教程 欢迎关注解锁更多精彩内容!