TS-基本类型, 变量,接口

93 阅读2分钟
let name1: string = `Gene`;
let age: number = 37;
let sentence:string = `Hello, my name is ${ name1 }. I'll be ${age+1} years old next month`
console.log(sentence)

//数组
let list: number[] = [1,2,3];
let list2: Array<number> = [1,2,3];


//元祖
let x:[string, number] = ['hello', 10];
console.log(x[1])

enum Color {Red, Green, Bule}

let c:Color = Color.Green;
console.log(c);
let t:string;

t = Color[2];
console.log(t);

let notsure: any = 4;
notsure = 'maybe a string instead';
// notsure = false;
//notsure.toFixed();

// let prettySure: object = 4;
// prettySure.toFixed();

//void类型
function warnUser(): void {
    alert("This is my warning message");
}

let unusable: void = undefined;

//Null Undefined

let u: undefined = undefined;
let n: null = null;

//Never 类型表示的是那些永不存在的值的类型例如,never类型是那些总是会抛出异常或根本就不会有返回值的函数表达式或箭头函数表达式的返回值类型; 
function error(message:string):never {
    throw new Error(message);
}

function fail() {
    return error("Something failed");
}

function infiniteLoop():never {
    while(true) {

    }
}

//类型断言, 可以理解为类型转换

// let  someValue: any = "this is a string";
// let strLength:number = (<string>someValue).length;

let  someValue: any = "this is a string";
let strLength: number = (someValue as string).length;

//不可变变量
const numlivesForCat = 9;
const kitty = {
    name:"Aurora",
    numLvives: numlivesForCat
}

//Error
// kitty = {
//     name: "Danielle",
//     numlives: numlivesForCat
// }

kitty.name = "Rory";
kitty.numLvives--;

//数组解构
let [a, b, d] = [1,2,3];
console.log(a,b, d)

function test([n1, n2]:Array<any>) {
    console.log(n1, n2);
}

test(["hello", 110]);

//对象解构
let {key,value} = {key:"K", value:"V"};

function test1({k, v}:{k:string, v:number}) {
    console.log(k,v);
}

test1({k:"h1", v:100});

//接口 interfaces

//demo

//这里定义了一个函数, labeledObj为形参名, 类型为一个对象, 并要求这个对象参数里有一个名为label,类型为string的属性
function printLabel(labeledObj:{label:string}) {
    console.log(labeledObj.label);
}

let myobj = {size:10, label:"Size 10 Object"};
printLabel(myobj);

//重写上面的例子 使用interface

interface LabelledValue {
    label:string;
}

//这里定义了一个函数,这里并非需要传入一个实现LabelledValue接口的对象,只会去关注值的外形, 只要传入的对象满足条件就可以
function printLabel1(labelledObj:LabelledValue) {
    console.log(labelledObj.label);
}
printLabel1(myobj);

//可选属性 代表属性可能为空值
interface SquareConfig {
    color?:string;
    width?:number;
}

function createSquare(config:SquareConfig) {
    console.log(config.color);
    console.log(config.width);
}
//此时config.width 打印结果为undefined 
createSquare({color:"black"});

//只读属性
interface Point {
    readonly x:number;
    readonly y:number;
}

let p1:Point = {x:10, y:10};
//p1.x = 5;  只读属性无法修改

//ReadonlyArray //只读数组

let arr:number[] = [1,2,3,4]
let ro:ReadonlyArray<number> = arr;
//ro[0]= 12;
//re.length = 100; 只读数组不能修改
//arr = ro;  只读数组不能赋值给平台数组
// 可以使用类型断言 也就是类型转换
arr = ro as number[];

//接口描述函数类型

interface SearchFunc {

    (source:string, subString:string):boolean;
}

let mySearch:SearchFunc;

mySearch = function(source:string, subString:string){
    return "".length > 0;
}
//对应的函数自需要参数列表类型匹配即可, 行参名不需要匹配

//类类型实现接口

interface ClockInterface {
    currentTime:Date;
    
}

class Clock implements ClockInterface {
    currentTime:Date;
    constructor(h:number, m:number) {
        this.currentTime = new Date;
    }
}