zoukankan      html  css  js  c++  java
  • TypeScript基础学习(待更新)

    TypeScript

    • [x] TypeScript的核心原则之一是对值所具有的结构进行类型检查
    • [x] 定义一个属性的时候,事先规定其结构与类型

    1.基础类型

    使用时:

    布尔值:

    	let isDone: boolean = false;
    

    数字:

    	let isNumber: number = 123;
    

    字符串:

    	let isString: string = 'false';
    

    数组:

    	let list: number[] = [123];
    	let list: Array<number> = [1, 2, 3];
    

    元组 Tuple:

    ​ 元组类型允许表示一个已知元素数量和类型的数组,各元素的类型不必相同。 比如,你可以定义一对值分别为 stringnumber类型的元组。

    	// Declare a tuple type
    	let x: [string, number];
    	// Initialize it
    	x = ['hello', 10]; // OK
    	// Initialize it incorrectly
    	x = [10, 'hello']; // Error
    

    ​ 当访问一个已知索引的元素,会得到正确的类型:

    	console.log(x[0].substr(1)); // OK
    	console.log(x[1].substr(1)); // Error, 'number' does not have 'substr'
    

    ​ 当访问一个越界的元素,会使用联合类型替代:

    	x[3] = 'world'; // OK, 字符串可以赋值给(string | number)类型
    
    	console.log(x[5].toString()); // OK, 'string' 和 'number' 都有 toString
    
    	x[6] = true; // Error, 布尔不是(string | number)类型
    

    ​ 联合类型是高级主题,我们会在以后的章节里讨论它。

    枚举:

    enum类型是对JavaScript标准数据类型的一个补充。 像C#等其它语言一样,使用枚举类型可以为一组数值赋予友好的名字。

    	enum Color {Red, Green, Blue}
    	let c: Color = Color.Green;
    

    ​ 默认情况下,从0开始为元素编号。 你也可以手动的指定成员的数值。 例如,我们将上面的例子改成从 1开始编号:

    	enum Color {Red = 1, Green, Blue}
    	let c: Color = Color.Green;
    

    ​ 或者,全部都采用手动赋值:

    	enum Color {Red = 1, Green = 2, Blue = 4}
    	let c: Color = Color.Green;
    

    ​ 枚举类型提供的一个便利是你可以由枚举的值得到它的名字。 例如,我们知道数值为2,但是不确定它映射到Color里的哪个名字,我们可以查找相应的名字:

    	enum Color {Red = 1, Green, Blue}
    	let colorName: string = Color[2];
    
    	console.log(colorName);  // 显示'Green'因为上面代码里它的值是2
    

    Any:

    ​ 有时候,我们会想要为那些在编程阶段还不清楚类型的变量指定一个类型。 这些值可能来自于动态的内容,比如来自用户输入或第三方代码库。 这种情况下,我们不希望类型检查器对这些值进行检查而是直接让它们通过编译阶段的检查。 那么我们可以使用 any类型来标记这些变量:

    	let notSure: any = 4;
    	notSure = "maybe a string instead";
    	notSure = false; // okay, definitely a boolean
    

    ​ 在对现有代码进行改写的时候,any类型是十分有用的,它允许你在编译时可选择地包含或移除类型检查。 你可能认为 Object有相似的作用,就像它在其它语言中那样。 但是 Object类型的变量只是允许你给它赋任意值 - 但是却不能够在它上面调用任意的方法,即便它真的有这些方法:

    	let notSure: any = 4;
    	notSure.ifItExists(); // okay, ifItExists might exist at runtime
    	notSure.toFixed(); // okay, toFixed exists (but the compiler doesn't check)
    	
    	let prettySure: Object = 4;
    	prettySure.toFixed(); // Error: Property 'toFixed' doesn't exist on type 'Object'.
    

    ​ 当你只知道一部分数据的类型时,any类型也是有用的。 比如,你有一个数组,它包含了不同的类型的数据:

    	let list: any[] = [1, true, "free"];
    
    	list[1] = 100;
    

    Void:

    ​ 某种程度上来说,void类型像是与any类型相反,它表示没有任何类型。 当一个函数没有返回值时,你通常会见到其返回值类型是 void

    	function warnUser(): void {
        	console.log("This is my warning message");
    	}
    

    ​ 声明一个void类型的变量没有什么大用,因为你只能为它赋予undefinednull

    	let unusable: void = undefined;
    

    Null 和 Undefined:

    ​ TypeScript里,undefinednull两者各自有自己的类型分别叫做undefinednull。 和 void相似,它们的本身的类型用处不是很大:

    	// Not much else we can assign to these variables!
    	let u: undefined = undefined;
    	let n: null = null;
    

    ​ 默认情况下nullundefined是所有类型的子类型。 就是说你可以把 nullundefined赋值给number类型的变量。

    ​ 然而,当你指定了--strictNullChecks标记,nullundefined只能赋值给void和它们各自。 这能避免 很多常见的问题。 也许在某处你想传入一个 stringnullundefined,你可以使用联合类型string | null | undefined。 再次说明,稍后我们会介绍联合类型。

    注意:我们鼓励尽可能地使用--strictNullChecks,但在本手册里我们假设这个标记是关闭的。

    Never:

    never类型表示的是那些永不存在的值的类型。 例如, never类型是那些总是会抛出异常或根本就不会有返回值的函数表达式或箭头函数表达式的返回值类型; 变量也可能是 never类型,当它们被永不为真的类型保护所约束时。

    never类型是任何类型的子类型,也可以赋值给任何类型;然而,没有类型是never的子类型或可以赋值给never类型(除了never本身之外)。 即使 any也不可以赋值给never

    ​ 下面是一些返回never类型的函数:

    	// 返回never的函数必须存在无法达到的终点
    	function error(message: string): never {
        	throw new Error(message);
    	}
    
    	// 推断的返回值类型为never
    	function fail() {
        	return error("Something failed");
    	}
    
    	// 返回never的函数必须存在无法达到的终点
    	function infiniteLoop(): never {
        	while (true) {
        	}
    	}
    

    Object:

    object表示非原始类型,也就是除numberstringbooleansymbolnullundefined之外的类型。

    ​ 使用object类型,就可以更好的表示像Object.create这样的API。例如:

    	declare function create(o: object | null): void;
    
    	create({ prop: 0 }); // OK
    	create(null); // OK
    
    	create(42); // Error
    	create("string"); // Error
    	create(false); // Error
    	create(undefined); // Error
    

    类型断言:

    ​ 有时候你会遇到这样的情况,你会比TypeScript更了解某个值的详细信息。 通常这会发生在你清楚地知道一个实体具有比它现有类型更确切的类型。

    ​ 通过类型断言这种方式可以告诉编译器,“相信我,我知道自己在干什么”。 类型断言好比其它语言里的类型转换,但是不进行特殊的数据检查和解构。 它没有运行时的影响,只是在编译阶段起作用。 TypeScript会假设你,程序员,已经进行了必须的检查。

    ​ 类型断言有两种形式。 其一是“尖括号”语法:

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

    ​ 另一个为as语法:

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

    ​ 两种形式是等价的。 至于使用哪个大多数情况下是凭个人喜好;然而,当你在TypeScript里使用JSX时,只有 as语法断言是被允许的。

    2.接口

    什么是接口?
    // 预先规定传入数据的结构及其属性
    	interface: example {
            a: string;
            b: number;
        }
    	funtion fn (val:example) {
            // ...
        }
    	let start = fn({a: 'a', b: 123}) // right
        let start1 = fn({a: '123'}) // error, 缺少属性b
        let start2 = fn({a: 123, b: 123}) // error, a是数字应该是字符串
    
    可选属性
    // 预先规定传入数据中可能存在的值
    	interface example {
        	a?: string,
        	b?: number
    	}
        function fn (val:example) {
            // ...
        }
    	let start = fn({a:'s'}) // right
    
    只读属性
    // 预先规定本接口规定的值,一旦被赋值就不能修改
    interface example {
        readonly a: string;
        readonly b: number;
    }
    let c:example = {a: 'a', b: 2}
    c.a = 'c' // error, a属性只读不能修改
    

    类似的,在typescript中有 ReadonlyArray 类型, 与只读属性相同一旦获得初始值便不能修改

    let example: number[] = [1, 2, 3, 4]
    let ro: ReadonlyArray<number> = a
    ro[0] = 12 // error
    ro.push(5); // error!
    ro.length = 100; // error!
    a = ro; // error!
    
    额外的属性检查
    interface example {
        a?: string;
        b?: number;
        cat?: string;
    }
    function fn (val:example) {
        // ...
    }
    let start = fn({a: 'a', b:2, cet: 'cat'}); // error, 在example中找不到cet属性
    
    
    //  如果想要在传入值中加入任意数量的任意属性,可以写做如下模样
    interface example1 {
        a?: string;
        b?: number;
        [listName:string]: any;
    }
    function fn1 (val:example1) {
        // ...
    }
    let start1 = fn1({a: 'a', b:2, cet: 'cat'}) // right
    
    函数类型
    // '接口'不仅仅能定义传入参数的结构与属性,还能规定函数返回值的类型,即'函数类型'
    interface example {
        (source: string, subString: string): boolean;
    }
    let fn:example;
    fn = function (source: string, subString: string):boolean {
        // ...
        return true;
    }
    
    可索引的类型(待添加理解)
    // '接口'还能描述 '通过索引得到的' 类型。
    // 可索引类型具有一个索引签名,他描述了对象索引的类型,还有相应的索引返回值类型。
    interface strArray {
        [index: number]: string;
    }
    let list:strArray;
    list = ['a', 'b']
    let str:string = list[0]
    
    类类型
    interface example {
        a?: string;
    }
    
    继承接口
    interface example {
        a?: string;
    }
    
    混合类型
    interface example {
        a?: string;
    }
    
    接口继承类
    interface example {
        a?: string;
    }
    
  • 相关阅读:
    实用机器学习 跟李沐学AI
    Explicitly drop temp table or let SQL Server handle it
    dotnettransformxdt and FatAntelope
    QQ拼音输入法 禁用模糊音
    (技术八卦)Java VS RoR
    Ruby on rails开发从头来(windows)(七)创建在线购物页面
    Ruby on rails开发从头来(windows)(十三)订单(Order)
    Ruby on rails开发从头来(windows)(十一)订单(Order)
    新员工自缢身亡,华为又站到了风口浪尖
    死亡汽油弹(Napalm Death)乐队的视频和来中国演出的消息
  • 原文地址:https://www.cnblogs.com/ww-523-99/p/14069810.html
Copyright © 2011-2022 走看看